我是模拟测试的新手。我正在努力学习,但找不到针对我的具体问题的答案。
我在其中一个控制器中有如下方法
@RequestMapping(value = /employer, method = RequestMethod.POST,
consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public HttpStatus saveEmployerInfo (@Valid @RequestBody EmployerInfo empInfo,
HttpServletRequest request)
{
String employeeId = (String) request.getAttribute(EMP_ATTRIBUTE);
employerService.processEmpInfo(empInfo, employeeId);
return HttpStatus.OK;
}
我正在尝试在 mockito 中执行以下操作:
EmployerInfo mockEmpInfo = mock(EmployerInfo.class);
HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
Controller mockController = mock(Controller.class);
when(mockController.saveEmployerInfo(Matchers.any(EmployerInfo.class), (HttpServletRequest) any(HttpServletRequest.class))).thenReturn(HttpStatus.OK);
HttpStatus responseStatus = mockController.saveEmployerInfo(Matchers.any(EmployerInfo.class),
(HttpServletReq(mockEmpInfo, mockHttpServletRequest);
assertEquals(HttpStatus.OK, responseStatus.OK);
我仍然遇到以下错误并且无法解决此问题:
java.lang.ClassCastException: org.hamcrest.core.IsInstanceOf cannot be cast to com.xxx.model.EmployerInfo