【发布时间】:2017-04-04 14:00:26
【问题描述】:
我们有一个 spring-boot 版本 1.5.2.RELEASE 的项目。
我们需要在 xml 中处理 hibernate 命名查询(java 注释中的命名查询不是我们的选择)。
为此,我们将所有hbm.xml 文件(包含这些命名查询)添加到src/main/resources 目录中。
当我们的应用程序运行时,这不是问题。命名查询被正确提取。
但是,当我们编写集成测试用例时,它无法识别命名查询。
我们得到:
未找到命名查询异常
下面是我们的测试用例代码:
@RunWith(SpringRunner.class)
@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyIntegrationTest {
@Autowired
private TestRestTemplate template;
@Test
public void checkRestService() throws Exception {
ResponseEntity<String> response = template.getForEntity("/hello/1", String.class);
assertTrue(response.getStatusCodeValue() == 200);
}
}
如果我们将hbm.xml 文件复制到src/test/resources directory 中,则hbm.xml 文件会被正确拾取并且测试会正确运行。
有没有直接从src/main/resouces文件夹中提取xml文件而我们不必复制这些文件?
【问题讨论】:
-
一个asked question 可以帮助你。
-
感谢您发布类似问题的链接。但是,我们能够在主应用程序中正确使用 Hibernate SessionFactory 并使用来自 hbm.xml 文件的命名查询。问题只出在编写集成测试用例时。
标签: java spring-boot spring-data-jpa spring-boot-test