首页 > 解决方案 > 如何使用 Junit 5 测试 Spring Boot Rest 控制器

问题描述

我很难找到关于如何为 Sprint Boot 实际编写 Junit5 测试代码的一致资源。现在,我有一个非常简单的控制器并尝试进行 Junit5 测试,但我什至无法从简单的休息调用中获得结果。假设返回一个字符串值数组。我认为主要问题是 MockHttpServletResponse 标头没有在其中附加端口,但我希望它可以动态添加,因此无论谁拉下代码,Junit5 都可以对其进行测试。

@RestController
public class CnlLetterController {

    
    @Autowired
    private CnlLetterService cnlLetterService;

    @Autowired
    private UserService userService;
    
    @Autowired
    private ServiceLetterService serviceLetterService;
    
    @GetMapping("/rest/cnl/templates")
    public List<String> findTemplates () {
        
        return cnlLetterService.getTemplates();
    }

    /* Other methods that don't matter right now */
}

测试控制器


@ActiveProfiles("dev")
@ExtendWith(SpringExtension.class)
@WebMvcTest(CnlLetterController.class)
class CnlLetterControllerTest {

    @MockBean
    private ClientRegistrationRepository crr;
    
    @MockBean
    private CnlLetterService cnlLetterService;
    
    @MockBean
    private UserService userService;
    
    @MockBean
    private ServiceLetterService serviceLetterService;
    
    @Autowired
    private MockMvc mockMvc;

    @Test
    void testFindTemplates() throws Exception {
        
        mockMvc.perform(MockMvcRequestBuilders.get("/pis/rest/cnl/templates")
                .accept(MediaType.APPLICATION_JSON))
                .andDo(print()).andExpect(status().isOk());
        
    }
}

Junit控制台输出


MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /pis/rest/cnl/templates
       Parameters = {}
          Headers = [Accept:"application/json"]
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = null

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 302
    Error message = null
          Headers = [Location:"https://localhost/pis/rest/cnl/templates"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = https://localhost/pis/rest/cnl/templates
          Cookies = []

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /pis/rest/cnl/templates
       Parameters = {}
          Headers = [Accept:"application/json"]
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = null

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 302
    Error message = null
          Headers = [Location:"https://localhost/pis/rest/cnl/templates"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = https://localhost/pis/rest/cnl/templates
          Cookies = []
2020-07-20 13:37:29.648  INFO 12868 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

标签: javaspring-bootjunit5

解决方案


推荐阅读