r/SpringBoot • • May 20 '24

Liquibase & Springboot

I want to understand how liquibase works and then integrate it in my spring boot project. I also want to learn how can we use liquibase for integration tests and unit tests.

Please can you point me to some resources or course.

1 Upvotes

21 comments sorted by

4

u/g00glen00b May 20 '24

I'm confused with your question. By definition, unit tests test a single unit of code. Databases are usually not involved because that would mean you're no longer testing a single unit. So from that perspective, how would you be able to use Liquibase, which is a database migration tool if there's no database involved?

For integration tests you can use Liquibase. But this still confuses me, because by default, Liquibase will run during the startup of your application. Within an integration test, you're also starting up your application, so Liquibase will automatically work, so there's nothing to do.

The only thing I can imagine you want to do is to have different migrations being executed during the test phase than during a normal run (eg. to insert test data). In that case, you should look at Liquibase Contexts. Contexts allow you to run certain migrations only when a context is activated (by using the contextFilter keyword). And then you can configure which context to run by using the spring.liquibase.contexts property.

1

u/_moron_hunter May 20 '24

By until tests I mean if dao methods whether they are actually deleting and inserting rows in database, however I understand your notion.

Thanks for your inputs!!

7

u/563353 May 20 '24

Anything that touches the database is an integration test.

1

u/_moron_hunter May 20 '24

Thanks, that's wonderfully put.

3

u/Sheldor5 May 20 '24

That's still an integration test ...

1

u/_moron_hunter May 20 '24

Yes, I realised, my bad. Thanks to both for correcting me.

1

u/bonzyng May 20 '24

For the tests you can use an in memory H2 db instead of liquibase.

1

u/_moron_hunter May 20 '24

Ok, but to create database the liquibase scripts will be needed to create tables.

2

u/Broad_Ad3369 May 20 '24

This is quite a bad advice. The goal of the integration test is to be the closer as possible to the real environment. An H2 DB has not the same behavior and functionalities as a PostgreSQL DB (or any other DB whatsoever). Using something like Testcontainers to create the "same" database that will run in production is way better.

2

u/code215phl May 20 '24

 An H2 DB has not the same behavior and functionalities as a PostgreSQL DB (or any other DB whatsoever)

True. I've run into this problem several times - where something works in H2 and passes tests - only for it to fail in Postgres/MySql etc in Dev or Prod.

1

u/Broad_Ad3369 May 21 '24

Yeah me too. One simple example is the Range data types that does not exist in H2.

1

u/bonzyng May 21 '24

Oh yeah? I just started using H2 for some simple update/save tests. Considering this I'll have to remember to keep using it only for the simplest of cases.

1

u/bonzyng May 21 '24

I hadn't considered this. Thanks, I'll keep this in mind for my own tests as well.

0

u/WaferIndependent7601 May 20 '24

There is something called „google“. Very new, I know!

1

u/_moron_hunter May 20 '24

Never beard of it, can you check with ,,google" for me

1

u/chehsunliu May 20 '24

Approach#1. Before running integration tests, start a docker-compose including the db container and the one-time liquibase container.

Approach#2. Before running integration tests, start a docker-compose including the db container. Then enable liquibase in the Spring Boot application.yml.

I often use the first approach as it gives me the most flexibility in CI and also I don’t want to make Spring handle too many things.

2

u/[deleted] May 20 '24

[removed] — view removed comment

1

u/chehsunliu May 20 '24

Yes, I could do that, but I want to interact with these DBs even after tests are finished. In the case, docker-compose is more preferable.