Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create liquibase changeset for integration tests in springboot?

I want mock data for integration tests by liquibase changeset, how to make that to not affect real database? I found partial idea from here, but I am using springboot and I hope there is simpler solution.

like image 592
Bartek Avatar asked Oct 31 '17 13:10

Bartek


People also ask

How do I create a changeset in Liquibase?

Running the generate-changelog command Instead of using a properties file, you can pass the necessary information from the command line. Note: If you want to create an SQL changelog file, add your database type name when specifying the changelog file: liquibase --changelog-file=mychangelog.

How do you integrate Liquibase?

Integrating Liquibase into your Spring Boot application is extremely easy. You just have to add the Liquibase Core to your classpath. That's all you need to do. The Liquibase integration will automatically load the master changelog file from the db changelog directory.

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.


2 Answers

Assume production changeset placed inside resources/db/changelog/changes, and there is a db.changelog-master.yaml in /db/changelog with following config

databaseChangeLog:
  - includeAll:
      path: db/changelog/changes

Place the testing changset inside test/resources/db/changelog/testchanges and create db.changelog-master.yaml in test/resources/db/changelog with following config

databaseChangeLog:
  - includeAll:
      path: db/changelog/changes
  - includeAll:
      path: db/changelog/testchanges

The test should pick up all changeset in two paths and run

like image 79
Kencourt Avatar answered Nov 11 '22 16:11

Kencourt


You can use liquibase's context parameter. For example create changeset which will have inserts loaded from sql file and specify the context for it. Something like this:

<changeSet id="test_data_inserts" author="me" context="test">
    <sqlFile path="test_data.sql" relativeToChangelogFile="true" />
</changeSet>

and in spring boot's application.properties for test specify the property liquibase.contexts=test.

like image 22
bilak Avatar answered Nov 11 '22 16:11

bilak