【问题标题】:How to write mockito for annotated parameter in function如何为函数中的注释参数编写模拟
【发布时间】:2019-02-06 18:16:03
【问题描述】:

我是模拟测试的新手。我正在努力学习,但找不到针对我的具体问题的答案。

我在其中一个控制器中有如下方法

@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

【问题讨论】:

  • 您的测试没有多大意义:1. 在调用 saveEmployerInfo 时似乎缺少),2. 一切都被模拟了,所以调用您想要在模拟上测试的方法实例势必会给你报错。

标签: java spring spring-boot mockito springmockito


【解决方案1】:

如果您想测试您的控制器,您必须在真实实例上进行,而不是在模拟实例上进行。假设您的控制器看起来像这样:

@Controller
public class EmployerController {
  @Autowire
  EmployerService employerService;

  @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;
  }
}

那么测试可能如下所示(使用 Juni4):

@RunWith(MockitoJUnitRunner.class)
public class EmployerControllerTest {

  @Mock
  private EmployerService employerService;

  @InjectMocks
  private EmployerController controller;

  @Test
  public void shouldReturnHttpStatusOk() {
    // Given
    EmployerInfo mockEmpInfo = mock(EmployerInfo.class);
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    String employerId = "employerId"
    when(mockHttpServletRequest.getAttribute(EMP_ATTRIBUTE)).willReturn(employerId);

    // When
    HttpStatus status = controller.saveEmployerInfo(mockEmpInfo, mockHttpServletRequest);

    // Then
    verify(employerService).processEmpInfo(mockEmpInfo, employerId); 
    assertEquals(HttpStatus.OK, status);
  }  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 2018-05-02
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    相关资源
    最近更新 更多