Java Junit测试用例使用mockito进行异常处理

Java Junit测试用例使用mockito进行异常处理,java,junit,mockito,Java,Junit,Mockito,我有一个类似于下面的方法,为此我必须为FileNotFoundException编写测试用例 public ResponseEntity<ChefAgent> getClientDetails(String clientName) { LogImpl.setLogger(className, "Entered RESTAPI method to getClientDetails"); ResponseEntity<ChefAgent&g

我有一个类似于下面的方法,为此我必须为FileNotFoundException编写测试用例

public ResponseEntity<ChefAgent> getClientDetails(String clientName) {

    LogImpl.setLogger(className,
            "Entered RESTAPI method to getClientDetails");
    ResponseEntity<ChefAgent> response = null;
    ResponseBean responseBean = null;
    ErrorBean errorBean = new ErrorBean();
    try {

        requestBean.setChefApi(ChefServiceProvider.getApi());
        requestBean.setService(NodeManagementConstants.CLIENT_DETAILS);
        requestBean.setClientName(clientName);
        responseBean = delegator.handleRequest(requestBean);

        if (null != responseBean.getChefAgent()) {
            response = new ResponseEntity<ChefAgent>(
                    responseBean.getChefAgent(), new HttpHeaders(),
                    HttpStatus.OK);
        } else {
            response = new ResponseEntity<ChefAgent>(
                    responseBean.getChefAgent(), new HttpHeaders(),
                    HttpStatus.BAD_REQUEST);
        }
        ChefServiceProvider.getClose();
    } catch (FileNotFoundException e) {
        LogImpl.setWarning(Segregator.class, e.getMessage());

        LogImpl.setWarning(className, "error occured");

        errorBean.setErrorCode(ErrorCode.FILE_NOT_FOUND_ERROR
                .getErrorCode());
        errorBean.setErrorMessage(ErrorCode.FILE_NOT_FOUND_ERROR
                .getMessage());
        ChefAgent agent = new ChefAgent();
        agent.setErrorBean(errorBean);
        response = new ResponseEntity<ChefAgent>(agent, new HttpHeaders(),
                HttpStatus.BAD_REQUEST);
        return response;
    }
    catch (CustomException e) {
        LogImpl.setWarning(Segregator.class, e.getMessage());
    }
    LogImpl.setLogger(Segregator.class,
            "Ended RESTAPI method to getClientDetails");

    return response;
}
但我得到的错误是

org.mockito.exceptions.misusing.MissingMethodInvocationException: when()需要的参数必须是“模拟的方法调用”


您是否模拟了ChefServiceProvider.getApi()?Mockito告诉您,您没有向其传递模拟对象

本机Mockito不允许您模拟静态方法。我可能会查看允许您执行此类操作的其他特性,并在将来进行调查,以允许您注入此类依赖项

PowerMock是一个扩展其他模拟库的框架,例如 EasyMock具有更强大的功能。PowerMock使用自定义的 类加载器和字节码操作,以支持模拟静态 方法、构造函数、最终类和方法、私有方法、, 删除静态初始值设定项等


当我使用PowerMock时,我使用它来围绕编写糟糕的代码创建一个单元测试,然后将代码重构为更好的模式,使用PowerMock来执行回归测试。重构后,我可以使用标准的Mockito实践,不再需要PowerMock。

如果您使用的是PowerMockito:

  • 您需要添加注释
    @PrepareForTest(ChefServiceProvider.class)
  • 您需要添加注释
    @RunWiths(PowerMockRunner.class)
  • 然后可以使用:
    PowerMockito.mockStatic(ChefServiceProvider.class)
  • 您不需要使用PowerMockito.when(ChefServiceProvider.getApi()).thenReturn(chefApi)-您可以删除此行
  • 您可以使用:
    PowerMockito.when(ChefServiceProvider.getApi()).thenthow(newfilenotfoundexception())

我正在使用下面的代码模拟方法PowerMockito.mockStatic(ChefServiceProvider.class);when(ChefServiceProvider.getApi()).thenReturn(chefApi);Mockito.when(ChefServiceProvider.getApi()).thenthow(newfilenotfoundexception());但在这里我得到了错误,比如--org.mockito.exceptions.misusing.MissingMethodInvocationException:when()需要一个参数,必须是“对模拟的方法调用”。这看起来像两个问题,我现在要分开,否则答案会很混乱记得对静态方法使用PowerMockito.when-而不是mockito.when。
@Test()
public void testGetClientDetailsFileNotFoundException() throws Exception {

    String clientName = "client";
    ChefAgent chefAgent = new ChefAgent();
    List<ChefClientBean> chefClientBean = new ArrayList<ChefClientBean>();
    chefClientBean.add(new ChefClientBean());
    chefAgent.setChefClientList(chefClientBean);
    responseBean.setChefAgent(chefAgent);
    ResponseEntity<ChefAgent> response;

    try {
        Mockito.doThrow(new RuntimeException()).when(ChefServiceProvider.getApi());
        response = cloudManagementHelper.getClientDetails(clientName);
        Assert.assertEquals(response.getStatusCode(), HttpStatus.BAD_REQUEST);
    } catch (FileNotFoundException e) {
        System.out.println("exception");
    } 
}
PowerMockito.mockStatic(ChefServiceProvider.class);
PowerMockito.when(ChefServiceProvider.getApi()).thenReturn(chefApi);

Mockito.when(ChefServiceProvider.getApi()).thenThrow(
        new FileNotFoundException());