Using Mockito for HTTP Client Using Mockito for HTTP Client json json

Using Mockito for HTTP Client


You might have to mock HttpClient and HttpResponse, if they're interfaces (although, depending on your library, you could use MockHttpClient or MockHttpResponse), but you shouldn't be mocking anything else.

Why?

The mock is establishing expected output behavior on classes that we cannot make concrete, or rather, classes that we want to behave in a certain way for this particular instance of a test. You want to ensure that you get the correct response back from a mock HttpClient, and that when response.getEntity() is called, that it gives back a meaningful HttpEntity. You can elect to mock that out or not; I personally wouldn't, as the mock doesn't add any extra value (except to perhaps verify that a particular method was called).

Everything else is a concrete implementation - you should be allowing the other objects to interact with the results of the previously mocked elements to ensure that they behave as they would if there were no mocks.

Actually...you really can't mock those unless you pass them in or inject them in some way. I would strongly discourage you from attempting to mock any newed objects in that method.

You don't specify what you're asserting, but I would expect that it's your JSONObject in some capacity. I'd assert that what you expected to be placed into it actually made it into the JSON object, and also verify that your mocked objects were called and invoked in the way you expected them to be.

Your annotation @Mock is not cascading, by the way - you have to annotate all mocked fields with @Mock, then either annotate the test class with @RunWith(MockitoJunitRunner.class), or use MockitoAnnotation.initMocks(this) (one or the other; both aren't required except under edge cases). If you choose the annotations, don't forget @InjectMocks on your test object.

Lastly, your when conditions do what I would expect them to do - those should be fine.


Yes, you might need all the when statements that you've mentioned.But instead of returning the mockInputStream, you could just return new ByteArrayInputStream( "{foo : 'bar'}".getBytes() )

Finally, you could verify that the json response object has a 'foo' property that has a value 'bar'.

That said, I'm not sure whether the given method is worth testing - since all it does it open streams and read data.


First understand meaning of Mocking.

1)Why we need mocking?

Suppose we don't want to call any original method and want that instead of that original method we should call a dummy method then we should go for mocking.

For e.g:

Mockito.when(mockHttpClient.execute(mockHttpPost)).thenReturn(mockHttpResponse)

This means whenever this execute() will be called then you will return your own prepared value instead of original mockHttpResponse.

So in your case prepare your stub object and mockit if you need that.Here you prepare your response(Note actual but dummy).

mockHttpResponse.setEntity(new Entity());mockHttpResponse.getEntity().setContent('{yourJsonString}');

So whenever your

  mockHttpClient.execute(mockHttpPost); //will be called

Then it will return the response that you prepared in your test method manually.

When your control comes to

  new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

Then you will get {yourJsonString} when response.getEntity().getContent() is called and let rest code do its functionality. At end your JSON stubbed object will be prepared.

Remember test cases are just for developers help.We can mock anything and return anything or any stub object.Just we write test case to check our control flow by passing expected and non expected values.

This will make your work easy.Now you want to mock your BufferReader class use this.

 BufferedReader bufferedReader = org.mockito.Mockito.mock(BufferedReader.class);  when(bufferedReader.readLine()).thenReturn("first line").thenReturn("second line"); org.junit.Assert.when(new Client(bufferedReader).parseLine()).thenEquals(IsEqual.equalTo("1"));