java junit mockito_java - Junit test case using mockito for Exception handling

-1

I have a method like below for which I have to write the test case for FileNotFoundException

public ResponseEntity getClientDetails(String clientName) {

LogImpl.setLogger(className,

"Entered RESTAPI method to getClientDetails");

ResponseEntity 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(

responseBean.getChefAgent(), new HttpHeaders(),

HttpStatus.OK);

} else {

response = new ResponseEntity(

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(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;

}

Here the method ChefServiceProvider.getApi() have some FileInputStream operation and for which it throws FileNotFoundException. I'm not able to create a scenario in mockito where I can test the catch block in method getClientDetails(String clientName). Tried something like below

@Test()

public void testGetClientDetailsFileNotFoundException() throws Exception {

String clientName = "client";

ChefAgent chefAgent = new ChefAgent();

List chefClientBean = new ArrayList();

chefClientBean.add(new ChefClientBean());

chefAgent.setChefClientList(chefClientBean);

responseBean.setChefAgent(chefAgent);

ResponseEntity 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");

}

}

But it gives error like

org.mockito.exceptions.misusing.NotAMockException: Argument passed to when() is not a mock!

Please let me know what is the correct way to do this.

I'm using below code to mock the method

PowerMockito.mockStatic(ChefServiceProvider.class);

PowerMockito.when(ChefServiceProvider.getApi()).thenReturn(chefApi);

Mockito.when(ChefServiceProvider.getApi()).thenThrow(

new FileNotFoundException());

But then I'm getting error like

org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'.

java

junit

mockito

|

this question

edited May 14 '14 at 19:17 asked May 14 '14 at 10:56

bagui 178 1 6 23      That looks like two questions now and I'd split up otherwise the answers are going to be confusing –

Brian Agnew May 14 '14 at 19:44

|

2 Answers

2

---Accepted---Accepted---Accepted---

If you're using PowerMockito:

You need to add annotation @PrepareForTest(ChefServiceProvider.class)

You need to add annotation @RunWiths(PowerMockRunner.class)

Then you can use: PowerMockito.mockStatic(ChefServiceProvider.class);

You don't need to use PowerMockito.when(ChefServiceProvider.getApi()).thenReturn(chefApi); - You can delete this line

You can use: PowerMockito.when(ChefServiceProvider.getApi()).thenThrow(new FileNotFoundException());

|

this answer answered May 14 '14 at 19:36

nnhthuan 760 1 5 20      Remember to use PowerMockito.when - not Mockito.when for static method. –

nnhthuan May 14 '14 at 19:53

|

Have you mocked ChefServiceProvider.getApi() ? Mockito is telling you that you've not passed it a mocked object.

Native Mockito won't let you mock static methods. I would perhaps check out PowerMock for additional features allowing you to do this sort of stuff, and investigate inversion-of-control for the future to allow you to inject such dependencies.

PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more

When I've used PowerMock I've used it to create a unit test around poorly written code, and then refactored the code to a better pattern, using PowerMock to perform regression tests. Once refactored I can then use standard Mockito practises and remove the need for PowerMock.

|

this answer answered May 14 '14 at 11:01

Brian Agnew 196k 22 242 347      I'm using below code to mock the method PowerMockito.mockStatic(ChefServiceProvider.class); PowerMockito.when(ChefServiceProvider.getApi()).thenReturn‌​(chefApi); Mockito.when(ChefServiceProvider.getApi()).thenThrow( new FileNotFoundException()); But here I'm getting error like --org.mockito.exceptions.misusing.MissingMethodInvocationExc‌​eption: when() requires an argument which has to be 'a method call on a mock'. –

bagui May 14 '14 at 19:12 1   Downvoted why ? –

Brian Agnew May 17 '14 at 19:48

|

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值