我有一个类似下面的方法,我必须为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;
}
这里方法ChefServiceProvider.getApi()有一些FileInputStream操作,并且它会抛出FileNotFoundException。我无法在mockito中创建一个场景,我可以在方法getClientDetails(String clientName)中测试catch块。试过类似下面的事情
@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");
}
}
但它给出了错误,如
org.mockito.exceptions.misusing.NotAMockException:传递给的参数 when()不是模拟!
我使用下面的代码来模拟方法
PowerMockito.mockStatic(ChefServiceProvider.class);
PowerMockito.when(ChefServiceProvider.getApi()).thenReturn(chefApi);
Mockito.when(ChefServiceProvider.getApi()).thenThrow(
new FileNotFoundException());
但是我得到了像
这样的错误org.mockito.exceptions.misusing.MissingMethodInvocationException: when()需要一个参数,该参数必须是模拟&#39;上的方法调用。
答案 0 :(得分:1)
如果您正在使用PowerMockito:
@PrepareForTest(ChefServiceProvider.class)
@RunWiths(PowerMockRunner.class)
PowerMockito.mockStatic(ChefServiceProvider.class);
PowerMockito.when(ChefServiceProvider.getApi()).thenReturn(chefApi);
- 您可以删除此行PowerMockito.when(ChefServiceProvider.getApi()).thenThrow(new FileNotFoundException());
答案 1 :(得分:0)
你有没有嘲笑ChefServiceProvider.getApi()
? Mockito告诉你,你没有把它作为一个模拟对象。
Native Mockito不会让你模拟静态方法。我可能会查看PowerMock以获取允许您执行此类操作的其他功能,并调查inversion-of-control以便将来允许您注入此类依赖项。
PowerMock是一个扩展其他模拟库的框架,例如 EasyMock具有更强大的功能。 PowerMock使用自定义 类加载器和字节码操作,以实现静态模拟 方法,构造函数,最终类和方法,私有方法, 删除静态初始化程序等等
当我使用PowerMock时,我已经使用它来创建一个编写不良代码的单元测试,然后使用PowerMock执行回归测试,将代码重构为更好的模式。一旦重构,我就可以使用标准的Mockito实践并消除对PowerMock的需求。