gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:16:30 25 4
gpt4 key购买 nike

我有以下测试:

@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com