6

I use liquibase to set up my database schema. I disable hibernate to create anything. Because of that, my import.sql is ignored. Is there any way to configure spring boot or liquibase or any other part to load test data after liquibase has created the tables?

Luka M.
  • 61
  • 1
  • 4
  • If you are using JUnit and have a base class for all your tests, you can have a method annotated with `@Before` (or `@BeforeClass` as the case may be) in your base class in which you can use your `EntityManager`. First obtain a JDBC `Connection` as `entityManager.unwrap(Connection.class)` (works only with JPA 2.0+). Then use the class described [in this post](http://stackoverflow.com/questions/1044194/running-a-sql-script-using-mysql-with-jdbc) to load and run your required SQL script. – manish Jul 11 '15 at 11:21

2 Answers2

0

If you need something crude (i.e, not for actual data migrations), you can use Spring's JDBC initializer in Spring Boot. In a nutshell, you'll need to:

  1. create a data-test.sql to load your data, and place it in your src/main/resources directory. For different environments, just use the naming convention data-{platform}.sql
  2. add applications-test.properties to src/main/resources with the following: spring.datasource.platform=test # this actives data-test.sql above spring.datasource.continueOnError=???? # depends on your needs
  3. to active the application-test.properties during your testing, make sure "test" is one of the profiles that's active during your integration test. One way to do this is to annotate your test class with @ActiveProfiles({"test", ...}).
ikumen
  • 11,275
  • 4
  • 41
  • 41
  • 2
    This does not answer the question. This solution only works if you are NOT using Flyway or Liquibase. The question is how to get a sql script to run after Liquibase runs in Spring Boot. Spring Boot turns off the Spring JDBC initializer if you are using Liquibase or Flyway with Spring Boot – bytor99999 Sep 28 '16 at 18:55
0

The simplest way seems to load the data with liquibase. You can do it with a normal Changeset (XML or JSON) or a Changeset in SQL-Format. The most common way is to load CSV-Data or run an existing SQL-File.

niels
  • 7,321
  • 2
  • 38
  • 58
  • 1
    You can load data with liquibase, but when you are using Spring Boot and liquibase and you want to create Integration tests that has pre-populated test data, just using liquibase to load the data isn't even an option. I have a 10MB sql file with sample data that we need to insert for the testing, I am not typing that into a changeset. – bytor99999 Sep 28 '16 at 19:10
  • 1
    @user1567291 see the extended answer. – niels Oct 06 '16 at 11:07