How execute `schema.sql` during spring boot test without embeded datasource configuration?

Cherry picture Cherry · Apr 26, 2017 · Viewed 9.2k times · Source

There is spring boot application with h2 database which is used as primary database. Also there is a resource/schema.sql wich is loaded at startup by spring boot.

But during integration tests with @SpringBootTest spring boot does not load this schema.sql. Instead it requires to setup embeded database while there is h2 db already.

Is there a way to execute schema.sql without embeded datasource configuration? And do it only once for all tests (e.g. using @Sql for schema creation for all test is not a solution)?

Answer

mr-m-jali picture mr-m-jali · May 3, 2017

Annotate your class or method with @Sql(scripts = "classpath:schema.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) where your schema.sql is on resources folder.

You need this if your tests are marked as integration tests. Spring boot will do this for you if you are running a unit test or just compiling with unit tests enabled which is the default.