问题
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)?
回答1:
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.
回答2:
Set this in your properties file and then rename schema.sql to schema-test.sql
spring.datasource.platform=test
Spring boot automatically configures the embedded database for you as long as you have the it in the classpath (h2, hsqldb or derby)
来源:https://stackoverflow.com/questions/43626269/how-execute-schema-sql-during-spring-boot-test-without-embeded-datasource-conf