How to test spring boot rest controller with Junit 5

Viewed 2048

I'm having an extremely hard time finding consistent resources on how to actually code Junit5 tests for Sprint Boot. Right now, I have have a very simple controller and my attempt at a Junit5 test, but I'm not even able to get the result back from a simple rest call. It's suppose to return an array of string values. I believe the main problem is the MockHttpServletResponse Header isn't appending a port on there, but I'd like it to be dynamically added so whoever pulls down the code can Junit5 test it just fine.

@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 */
}

TestController


@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 console output


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'
0 Answers
Related