【发布时间】:2021-04-23 19:50:18
【问题描述】:
我的单元测试看到org.hibernate.LazyInitializationException: could not initialize proxy [org.openapitools.entity.MenuItem#5] - no Session。我不确定他们为什么希望在单元测试中进行会话。我正在尝试为实现 RESTful API 的 Controller 类的单元测试写入内存中的 h2 数据库。我没有使用任何模拟对象进行测试,因为我想测试实际的数据库事务。这在我使用 Spring-Boot 版本 1.x 时效果很好,但在我移到版本 2 时就坏了。(我不确定这是否是导致测试中断的原因,因为我做了很多其他更改。我的观点是我的代码已经通过了这些测试。)
我的存储库扩展了 JPARepository,所以我使用标准的 Hibernate 接口。
在 StackOverflow 上有很多关于这个问题的答案,但很少有人描述我可以与 Spring-Data 一起使用的解决方案。
附录:下面看一下单元测试:
@Test
public void testDeleteOption() throws ResponseException {
MenuItemDto menuItemDto = createPizzaMenuItem();
ResponseEntity<CreatedResponse> responseEntity
= adminApiController.addMenuItem(menuItemDto);
final CreatedResponse body = responseEntity.getBody();
assertNotNull(body);
Integer id = body.getId();
MenuItem item = menuItemApiController.getMenuItemTestOnly(id);
// Hibernate.initialize(item); // attempted fix blows up
List<String> nameList = new LinkedList<>();
for (MenuItemOption option : item.getAllowedOptions()) { // blows up here
nameList.add(option.getName());
}
assertThat(nameList, hasItems("pepperoni", "olives", "onions"));
// ... (more code)
}
我的测试 application.properties 有这些设置
spring.datasource.url=jdbc:h2:mem:pizzaChallenge;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=pizza
spring.datasource.password=pizza
spring.jpa.show-sql=true
【问题讨论】:
-
你能发布一些代码吗?
标签: java spring-boot hibernate spring-data