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

烂漫一生 提交于 2019-12-08 05:04:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!