java - spring boot 测试加载在 java/resources 中定义的 schema.sql 而不是 test/resources

标签 java spring spring-boot spring-boot-test

我有以下测试:

@RunWith(SpringRunner.class)
@ContextConfiguration
@SpringBootTest(classes = {AccountsServerTest.class, PostgresSQLTestDbConfig.class})
@ActiveProfiles("test")
public class NodeRepositoryTest {
......
}

而且我在 src/main/resources 下有 schema.sql 和 data.sql 脚本,它们应该在应用程序启动时运行。

我在 src/test/resources/ 下还有另外两个 sql 文件,我想在 NodeRepositoryTest 启动时运行它们。

但是,由于某些原因,当我启动 NodeRepositoryTest 时,src/main/resources 中的脚本被执行。

也许以前有人遇到过同样的问题?

如果有任何帮助,我将不胜感激

谢谢

最佳答案

您可以手动定义应用于您的测试方法/测试类的.sql 脚本。

请看官方文档 (https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html) 中的以下示例:

@Test
@Sql({"/test-schema.sql", "/test-user-data.sql"})
public void userTest {
    // execute code that relies on the test data
}

关于java - spring boot 测试加载在 java/resources 中定义的 schema.sql 而不是 test/resources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48547155/

相关文章:

java - 将 JMS 队列添加到 Jboss 7

java - 如何使用类名 Autowiring Spring 服务?

java - Autowiring 字段为空,但在构造函数 Autowiring 时不是

spring-security - 登录成功后如何设置重定向?

spring - CDI @ViewScoped bean 功能不可用

spring - 有没有办法在 Spring 批处理中捕获 SkipLimitExceedException

Java多线程问题

java - 如何在Version.Lucene_35中创建新的Lucene文档?

java - Java类型不匹配?

java - 使用 spring-cloud-starter-netflix-eureka-client 依赖项在 pom.xml 中出现错误