0

My controller has this code

@RequestMapping(value = "/getUserByEmailId/{emailId}", method = GET, produces = "application/json")
public ResponseEntity<UserRegistrationResponse> getUserByEmailId(@PathVariable String emailId) throws ServiceException {
    return new ResponseEntity(generateUserOutput(userService.findUserByEmail(emailId)), OK);
}

My test has this:

@Test
public void testGetUserByEmailId() throws Exception {
    when(userService.findUserByEmail(any())).thenReturn(userOutput);
    MvcResult mvcResult = performGet("/user/getUserByEmailId/{emailId}", userRegistrationRequest.getEmail());
    assertEquals(200, mvcResult.getResponse().getStatus());
}

private MvcResult performGet(String path, String pathVariable) throws Exception {
    return mockMvc.perform(get(path,pathVariable)
            .accept(APPLICATION_JSON))
            .andDo(print())
            .andReturn();
}

This is returning HTTP status code 406.

MockHttpServletRequest:
  HTTP Method = GET
  Request URI = /user/getUserByEmailId/testEmailId@test.com
   Parameters = {}
      Headers = {Accept=[application/json]}
         Body = <no character encoding set>
Session Attrs = {}

Handler:
         Type = com.nz.unicorn.web.controller.UserController
       Method = public org.springframework.http.ResponseEntity<com.nz.unicorn.web.response.UserRegistrationResponse> com.nz.unicorn.web.controller.UserController.getUserByEmailId(java.lang.String) throws com.nz.unicorn.service.exception.ServiceException

Async:
Async started = false
 Async result = null

Resolved Exception:
         Type = org.springframework.web.HttpMediaTypeNotAcceptableException

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

FlashMap:
   Attributes = null

MockHttpServletResponse:
       Status = 406
Error message = null
      Headers = {}
 Content type = null
         Body = 
Forwarded URL = null
Redirected URL = null
      Cookies = []

Is there anything I can do? I have seen a lot of questions without an answer that I can use. Do note, mine is not a spring test, it's a unit test.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Pavanraotk
  • 1,097
  • 4
  • 15
  • 33

1 Answers1

0

This is a workaround I did, changed the controller as follows and it works:

@RequestMapping(value = "/getByEmailId/{emailId}", method = RequestMethod.GET, produces = "application/json")
@ResponseStatus(OK)
public UserRegistrationResponse getUserByEmailId(@PathVariable String emailId) throws ServiceException {
    return generateUserOutput(userService.findUserByEmail(emailId));
}

This solved, don't know why ResponseEntity still didn't work though.

Pavanraotk
  • 1,097
  • 4
  • 15
  • 33