【发布时间】:2021-05-01 22:33:27
【问题描述】:
我正在尝试本教程中的一些单元测试 Spring Boot MVC 示例 - https://spring.io/guides/gs/testing-web/。我的项目有 Spring Boot MVC 和 JPA (https://github.com/shankarps/SfgUdemyRecipes)
被测试的控制器检索一个配方实体列表,这些实体与类别实体具有多对多映射。
@RequestMapping({"", "/", "/index"})
public String getIndexPage(Model model){
log.info("Getting all recipes");
model.addAttribute("recipes", recipeService.getAllRecipes());
return "index";
}
这个控制器在 Spring Boot 应用程序启动时工作。食谱列表显示在浏览器中。
这个带有 SpringBootTest 和 TestRestTemplate 的 test case 按预期工作。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IndexControllerHttpTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testIndexUrl(){
String response = this.restTemplate.getForObject("http://localhost:"+port+"/", String.class);
}
}
但是,当我运行 test case with MockMVC 以将控制器作为 MVC 对象(使用 @AutoConfigureMockMvc)进行测试时,没有 Http 服务器,我得到 org.hibernate.LazyInitializationException 异常。
@SpringBootTest
@AutoConfigureMockMvc
public class IndexControllerMockMVCTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testIndexControllerView() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(status().isOk());
}
}
这里是完整的异常堆栈跟踪:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.shankar.sfg.recipes.recipes.domain.Category.recipes, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:606)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:218)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:585)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149)
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:327)
at java.lang.String.valueOf(String.java:2994)
这看起来像是事务范围的问题。任何人都可以帮助找到根本原因吗?与在 MVC 层测试(@SpringBootTest 和 @AutoConfigureMockMvc)相比,作为一个完整的 Web 应用程序(使用 @SpringBootTest)测试 Spring 应用程序上下文需要哪些额外的配置?
【问题讨论】:
标签: java spring-boot unit-testing spring-boot-test mockmvc