【问题标题】:What is the cause of LazyInitializationException when testing Spring Boot web app with MockMvc and AutoConfigureMockMvc使用 MockMvc 和 AutoConfigureMockMvc 测试 Spring Boot Web 应用程序时出现 LazyInitializationException 的原因是什么
【发布时间】: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


    【解决方案1】:

    解决方案是将@Transactional 添加到调用Mock MVC 的Test 方法中。

    @Test
    @Transactional
    public void testIndexControllerView() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(status().isOk());
    }
    

    此答案涉及单元测试 JPA 存储库对象的类似问题。

    LazyInitializationException: could not initialize proxy - no Session

    我还不确定为什么在没有 HTTP 服务器的情况下测试控制器时需要事务范围。

    【讨论】:

      猜你喜欢
      • 2019-02-21
      • 2015-07-22
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 2018-05-25
      • 2017-08-26
      相关资源
      最近更新 更多