You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/data-initialization.adoc
+51Lines changed: 51 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -65,6 +65,8 @@ This will defer data source initialization until after any `EntityManagerFactory
65
65
If you are using a <<howto#howto.data-initialization.migration-tool,Higher-level Database Migration Tool>>, like Flyway or Liquibase, you should use them alone to create and initialize the schema.
66
66
Using the basic `schema.sql` and `data.sql` scripts alongside Flyway or Liquibase is not recommended and support will be removed in a future release.
67
67
68
+
If you need to initialize test data using a higher-level database migration tool, please see the sections about <<howto#howto.data-initialization.migration-tool.flyway-tests, Flyway>> and <<howto#howto.data-initialization.migration-tool.liquibase-tests, Liquibase>>.
69
+
68
70
69
71
70
72
[[howto.data-initialization.batch]]
@@ -185,6 +187,55 @@ See {spring-boot-autoconfigure-module-code}/liquibase/LiquibaseProperties.java[`
If you want to create Flyway migrations which populate your test database, place them in `src/test/resources/db/migration`.
193
+
A file named, for example, `src/test/resources/db/migration/V9999__test-data.sql` will be executed after your production migrations and only if you're running the tests.
194
+
You can use this file to create the needed test data.
195
+
This file will not be packaged in your uber jar or your container.
If you want to create Liquibase migrations which populate your test database, you have to create a test changelog which also includes the production changelog.
202
+
203
+
First, you need to configure Liquibase to use a different changelog when running the tests.
204
+
One way to do this is to create a Spring Boot `test` profile and put the Liquibase properties in there.
205
+
For that, create a file named `src/test/resources/application-test.properties` and put the following property in there:
This changelog will be used when the tests are run and it will not be packaged in your uber jar or your container.
231
+
It includes the production changelog and then declares a new changeset, whose `runOrder: last` setting specifies that it runs after all the production changesets have been run.
232
+
You can now use for example the https://docs.liquibase.com/change-types/insert.html[insert changeset] to insert data or the https://docs.liquibase.com/change-types/sql.html[sql changeset] to execute SQL directly.
233
+
234
+
The last thing to do is to configure Spring Boot to activate the `test` profile when running tests.
235
+
To do this, you can add the `@ActiveProfiles("test")` annotation to your `@SpringBootTest` annotated test classes.
236
+
237
+
238
+
188
239
[[howto.data-initialization.dependencies]]
189
240
=== Depend Upon an Initialized Database
190
241
Database initialization is performed while the application is starting up as part of application context refresh.
0 commit comments