Java Junit测试用例使用mockito进行异常处理
我有一个类似于下面的方法,为此我必须为FileNotFoundException编写测试用例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
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())代码>
@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());