-2

I have a class which contains a method which I want to test. Here's the class.

class classOne {
private static boolean doneThis = false;
methodOne() {
 CloseableHttpResponse response = SomeClass.postData(paramOne, paramTwo);
                log.info("res - {}", response.getStatusLine().getStatusCode());
                doneThis = true;
}
}

Now, I want to mock the response.getStatusLine().getStatusCode() part using PowerMockito.

How can I acheive this? This is what I have done, but it is (the 2nd line below) getting NullPointerException.

CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
PowerMockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);

This is how I am mocking the Someclass.postData ->

PowerMockito.mockStatic(SomeClass.class);
ParamOne paramOne = new ParamOne(..);
// same for paramTwo    
   PowerMockito.when(SomeClass.postData(paramOne,paramTwo)).thenReturn(response);

Updated code:

CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
StatusLine statusLine = PowerMockito.mock(StatusLine.class);
PowerMockito.when(response.getStatusLine()).thenReturn(statusLine);
PowerMockito.when(statusLine.getStatusCode()).thenReturn(200);

Problem is, the mocked getStatusCode() is returning the expected value in the Test method - but in case of the actual class, the lines next to it are not being covered, i.e, the test is failing at that point. Workaround?

awesomemypro
  • 531
  • 1
  • 11
  • 32
  • Please extend your code and your test to include a *running example* and not just some snippets of code to enable others running it and suggesting improvements. – Smutje Jan 08 '20 at 10:14
  • 1
    `ParamOne paramOne = new ParamOne(..);` - you are creating these objects in your test and expect the productive objects being "equal" to the ones creating in your test? This sounds suspicious, *please* finally complete your example code, this is still neither runnable nor comprehensible. – Smutje Jan 08 '20 at 11:14
  • They are just String. I wrote "param" only for giving an example. – awesomemypro Jan 08 '20 at 12:08

1 Answers1

2

Of course you first have to mock

response.getStatusLine()

otherwise the default return value is null and calling .getStatusCode() on null leads to a NullPointerException.

CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
? statusLine = PowerMockito.mock(?.class);
PowerMockito.when(response.getStatusLine()).thenReturn(statusLine);
PowerMockito.when(statusLine.getStatusCode()).thenReturn(200);

Replace ? with the actual class of statusLine.

Smutje
  • 17,733
  • 4
  • 24
  • 41
  • Thanks a lot. How can I check the "doneThis" variable? – awesomemypro Jan 08 '20 at 09:22
  • As long is it is not visible from outside - why do you care? – Smutje Jan 08 '20 at 09:24
  • I just tried out the code you suggested above. It is not throwing the NullPointerException anymore, but the next line is not being covered. So, probably it is breaking on the same line. – awesomemypro Jan 08 '20 at 10:00
  • "the next line is not being covered" - what is that supposed to mean? Also, based on your code `SomeClass.postData(paramOne, paramTwo);` creates the response. Where do you mock this call in your example test? – Smutje Jan 08 '20 at 10:11
  • Added in the question description. – awesomemypro Jan 08 '20 at 10:40
  • Not working. The response.getStatusLine() is where the test is failing probably. I printed out this part and the coverage stopped there. – awesomemypro Jan 16 '20 at 07:08
  • Why didn't you update your question with your new code to let others test it and properly help? – Smutje Jan 16 '20 at 08:24