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.
3 Answers
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
.

- 8,008
- 26
- 77
- 177

- 4,526
- 3
- 35
- 75
-
2This solution doesn't allow to keep artifacts related to testing in a separate file & folder which is not good. I would like to have a separate liquibase file which will be virtually merged into main liquibase file when running tests. – Krzysztof Tomaszewski Oct 24 '18 at 08:23
-
I don't understand what you mean by that. You can have separate changelog for tests and include whatever you want in there. – bilak Oct 25 '18 at 09:21
-
The reason is simple: I don't want files being a part of application binary which is going to run on production to be messed by artifacts related to tests (unit tests, integration tests, etc.). – Krzysztof Tomaszewski Oct 25 '18 at 14:02
-
create a question for that and I can help you with it. – bilak Oct 25 '18 at 14:04
-
4Actually I found the solution in meantime: application properties file for tests should override liquibase file name, there should be a liquibase file in folder 'test/resources' which should include liquibase file from 'main/resources' and it can contain some change-sets for inserting data for tests. – Krzysztof Tomaszewski Oct 25 '18 at 15:15
-
3@KrzysztofTomaszewski maybe you could write your solution of linking the yaml changelog from main/resources in test/resources, cause i have the same issue now. It would be very helpful – Anton Barinov Nov 16 '18 at 12:09
-
1Yes, please @KrzysztofTomaszewski share your solution. I'm with the same need as you – sebasira Jan 04 '19 at 17:41
-
Here is the suggestion from Liquibase, using contexts : https://docs.liquibase.com/concepts/changelogs/attributes/contexts.html#using-contexts-for-test-data. However be careful to exclude it in production, with `contexts=!test`. It's easy to make mistakes... – Guillaume Husta Apr 01 '22 at 20:00
-
Otherwise, you can use _labels_ to have a different behaviour than with _contexts_, maybe less prone to errors. See : https://docs.liquibase.com/concepts/changelogs/attributes/labels.html. – Guillaume Husta Apr 01 '22 at 20:07
-
In fact, if a label is not specified, then **ALL labels** will be executed. So you have to specify `--labels=""` to have a different behaviour... – Guillaume Husta Apr 01 '22 at 20:41
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

- 205
- 2
- 7
-
1It works but be careful, all migrations from _changes_ well be executed, **then** those inside _testchanges_. Which can be a problem on the long term. If a script from _testchanges_ must be executed before a script from _changes_, it doesn't seem to be possible. See doc : https://docs.liquibase.com/concepts/changelogs/attributes/includeall.html – Guillaume Husta Apr 01 '22 at 20:25
Using final solution by @Krzysztof Tomaszewski
application properties file for tests should override liquibase file name, there should be a liquibase file in folder 'test/resources' which should include liquibase file from 'main/resources' and it can contain some change-sets for inserting data for tests.
I ended up with following configuration:
change-log: "classpath:db/liquibase-changelog-test.xml" //(1)
search-path: "./src/test/resources,./src/main/resources" //(2)
(1) rewrite change-log file property (2) rewrite search-path property
and liquibase-changelog-test.xml
looks like this:
<include file="db/liquibase-changelog.xml"/> //changelog file in /src/main/resources (creating schema etc.)
<include file="changes/01-insert-data.xml" relativeToChangelogFile="true"/> //changes file in /src/test/resources/db, file is relative to liquibase-changelog-test.xml file (inserting data for tests)

- 792
- 7
- 19