我的控制器有此代码
@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);
}
我的测试有这个:
@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();
}
这将返回HTTP状态代码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 = []
我能做些什么吗?我看到了很多问题,没有可以使用的答案。请注意,我的不是弹簧测试,而是单元测试。
答案 0 :(得分:0)
这是我所做的一种解决方法,按如下所示更改了控制器,并且可以正常工作:
set.seed
这解决了,不知道为什么ResponseEntity仍然不能正常工作。