Spring Boot 测试配置
SpringBoot test configuration
我开始学习Spring 2.1.0版本的Boot,做了一个简单的应用Controller->Service->Repository->Database(H2)。非常非常简单的应用程序,只需从 Spring Boot.
开始
我读过在那个框架中我可以在 src/resources 下添加一个文件 data.sql 其中我可以定义 insert/update 等,在 运行 之后我的应用程序将存储数据。一切正常,但是当我想为我的服务编写测试时,只是为了检查我的存储库是否正常工作(我为了学习目的测试数据库)我看到当我 运行 我在我的数据库中的测试时已经有值来自 data.sql,但我在测试文件夹下没有 data.sql。它在 src.
下
也许有人知道,如何配置项目不从 src/resources 中获取此 data.sql?可能吗?也许我必须添加更多注释?
**** 编辑 ****
您可以在 test/resources
中创建测试 application-test.properties
并像这样在测试中覆盖:
@RunWith(SpringRunner.class)
@SpringApplicationConfiguration(classes = ExampleApplication.class)
@TestPropertySource(locations="classpath:test.properties")
public class ExampleApplicationTests {
}
然后在您新创建的 application-test.properties
文件块中 运行 您的脚本:
spring.datasource.initialization-mode=never
我开始学习Spring 2.1.0版本的Boot,做了一个简单的应用Controller->Service->Repository->Database(H2)。非常非常简单的应用程序,只需从 Spring Boot.
开始我读过在那个框架中我可以在 src/resources 下添加一个文件 data.sql 其中我可以定义 insert/update 等,在 运行 之后我的应用程序将存储数据。一切正常,但是当我想为我的服务编写测试时,只是为了检查我的存储库是否正常工作(我为了学习目的测试数据库)我看到当我 运行 我在我的数据库中的测试时已经有值来自 data.sql,但我在测试文件夹下没有 data.sql。它在 src.
下也许有人知道,如何配置项目不从 src/resources 中获取此 data.sql?可能吗?也许我必须添加更多注释?
**** 编辑 ****
您可以在 test/resources
中创建测试 application-test.properties
并像这样在测试中覆盖:
@RunWith(SpringRunner.class)
@SpringApplicationConfiguration(classes = ExampleApplication.class)
@TestPropertySource(locations="classpath:test.properties")
public class ExampleApplicationTests {
}
然后在您新创建的 application-test.properties
文件块中 运行 您的脚本:
spring.datasource.initialization-mode=never