Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在没有嵌入式数据源配置的情况下,如何在spring启动测试期间执行`schema.sql`?_Java_Spring_Spring Boot_Embedded Database_Spring Boot Test - Fatal编程技术网

Java 在没有嵌入式数据源配置的情况下,如何在spring启动测试期间执行`schema.sql`?

Java 在没有嵌入式数据源配置的情况下,如何在spring启动测试期间执行`schema.sql`?,java,spring,spring-boot,embedded-database,spring-boot-test,Java,Spring,Spring Boot,Embedded Database,Spring Boot Test,有一个spring启动应用程序,其中h2数据库用作主数据库。还有一个resource/schema.sql,它在spring boot启动时加载 但是在使用@SpringBootTest进行集成测试期间,springboot不会加载这个schema.sql。相反,它需要在已有h2db的情况下设置嵌入式数据库 有没有一种方法可以在没有嵌入式数据源配置的情况下执行schema.sql?并且对所有测试只执行一次(例如,使用@Sql为所有测试创建架构不是解决方案)在属性文件中设置此选项,然后将schem

有一个spring启动应用程序,其中h2数据库用作主数据库。还有一个
resource/schema.sql
,它在spring boot启动时加载

但是在使用
@SpringBootTest
进行集成测试期间,springboot不会加载这个
schema.sql
。相反,它需要在已有
h2
db的情况下设置嵌入式数据库


有没有一种方法可以在没有嵌入式数据源配置的情况下执行
schema.sql
?并且对所有测试只执行一次(例如,使用
@Sql
为所有测试创建架构不是解决方案)

在属性文件中设置此选项,然后将schema.Sql重命名为schema-test.Sql
spring.datasource.platform=test


只要在类路径(h2、hsqldb或derby)中有嵌入式数据库,Spring boot就会自动为您配置嵌入式数据库。

使用
@Sql注释您的类或方法(scripts=“classpath:schema.Sql”,executionPhase=Sql.executionPhase.BEFORE\TEST\method)
,其中schema.Sql位于资源文件夹中


如果您的测试被标记为集成测试,则需要此选项。如果您正在运行单元测试,或者只是在启用单元测试的情况下编译(这是默认设置)。

参考指南中对此进行了解释。提示查找
spring.datasource.initialize
属性。
spring.datasource.initialize
在测试中不起作用:(如果它不起作用,您在测试中没有正确地使用Spring Boot…您有没有想过如何在所有测试中只运行一次它?