Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: How to setup test data with liquibase in unit test

Tags:

I'm trying to setup the database schema and some test data with liquibase for some tests. Each test has a separate changelog which setup the schema and some specific data for the test.

In order to make my tests working, I need to drop the schema before each test and fill it with new test data. However, it seems that this is not working because some tests are failing because the old test data is still available. I think something with my configuration is not correct. How can I force liquibase to drop the schema before each test?

My tests look as following:

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MyTestConfig.class) @TestPropertySource(properties = "liquibase.change-log=classpath:changelog/schema-with-testdata.xml") public class MyRepositoryTest { 

The config for the tests looks as follows:

@SpringApplicationConfiguration @Configuration @EnableAutoConfiguration @ComponentScan("com.mypackage") @EntityScan(basePackages = { "com.mypackage.domain" }) @EnableJpaRepositories(basePackages = { "com.mypackage.domain", "com.mypackage.infra.persistence" }) public class MyTestConfig { 

And the application.properties under src/main/test/resources is

liquibase.drop-first=true spring.jpa.hibernate.ddl-auto=none 
like image 926
René Winkler Avatar asked Mar 14 '16 20:03

René Winkler


People also ask

How do you use Liquibase with h2?

Install Liquibase. Create a Liquibase project folder to store all Liquibase files. You can do this manually or with the init project command. Create a new Liquibase properties file or use the liquibase.

How do you test for Liquibase?

Running via Maven From the root of the liquibase repository, run mvn test which will build the project and run all the tests. If any tests failed, you'll see it say “BUILD FAILED” at the end and the “T E S T S” section will list the failing tests.


1 Answers

There is a spring.liquibase.dropFirst config property. Maybe this is what you're looking for?

like image 180
MemLeak Avatar answered Oct 26 '22 23:10

MemLeak