【问题标题】:spring boot test loads schema.sql defined in java/resources instead of test/resourcesspring boot 测试加载在 java/resources 中定义的 schema.sql 而不是 test/resources
【发布时间】:2018-01-31 16:41:08
【问题描述】:

我有以下测试:

@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 的脚本。

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

非常感谢任何帮助,

谢谢

【问题讨论】:

  • 你有什么解决办法吗?

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


【解决方案1】:

您可以手动定义应该应用于您的测试方法/测试类的.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
}

【讨论】:

  • 这些文件的路径,是相对于哪个文件夹?
  • 这应该是classpath,你可以阅读@Sql的Javadoc来仔细检查。
猜你喜欢
  • 2011-02-24
  • 2013-09-04
  • 2010-11-30
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多