Using Mockito for an HTTP Client

I have an enveloped JSON OBJECT, but you need to make fun of the following: Mockito:

HttpResponse response = defaultHttpClient.execute(postRequest); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder result = new StringBuilder(); while ((line = rd.readLine()) != null) { result.append(line); } JSONObject jsonResponseObject = new JSONObject(result.toString()); 

I created the following Mocks:

 @Mock private HttpClient mockHttpClient; private HttpPost mockHttpPost; private HttpResponse mockHttpResponse; private HttpEntity mockHttpEntity; private InputStream mockInputStream; private InputStreamReader mockInputStreamReader; private BufferedReader mockBufferedReader; 

And the following when instructions:

  Mockito.when(mockHttpClient.execute(mockHttpPost)).thenReturn(mockHttpResponse); Mockito.when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity); Mockito.when(mockHttpEntity.getContent()).thenReturn(mockInputStream); 

Question: Do I have to create all these instructions β€œwhen”, and if so, which ones do I need to create in order to be able to access the cropped JSON?

Any suggestions pls?

thanks

+6
source share
3 answers

You may have to mock HttpClient and HttpResponse if they are interfaces (although, depending on your library, you can use MockHttpClient or MockHttpResponse ), but you should not taunt anything else.

Why?

The layout sets the expected behavior of the output on classes that we cannot make concrete, or rather, classes that we want to conduct in a specific way for this particular test instance. You want you to return the correct answer from mock HttpClient , and when response.getEntity() is called, it returns a meaningful HttpEntity . You can choose to mock it or not; I personally will not, as the layout does not add any additional value (except that it may verify that a particular method has been called).

Everything else is a concrete implementation β€” you must allow other objects to interact with the results of the previously mocked elements to ensure that they will behave as if they were not mocking.

Actually ... you really cannot mock them unless you pass them on or introduce them in any way. I would strongly discourage you from trying to scoff at any new ed objects in this method.

You do not indicate what you are claiming, but I expect it to be your JSONObject in some capacity. I would say that what you expected to be placed in it actually turned it into a JSON object, and also checked that your mocking objects were called and called the way you expected them to be.

Your @Mock annotation @Mock not cascade, by the way, you must annotate all manufactured fields using @Mock , or either annotate the test class using @RunWith(MockitoJunitRunner.class) , or use MockitoAnnotation.initMocks(this) (one or the other; both; they are not required, except in cases of edges). If you select annotations, don't forget @InjectMocks on the test object.

Finally, your when conditions do what I expect from them - everything should be fine.

+6
source

Yes, you may need all the statements you mentioned. But instead of returning mockInputStream you can simply return new ByteArrayInputStream( "{foo : 'bar'}".getBytes() )

Finally, you can verify that the json response object has a property of β€œfoo” that has a value of β€œbar”.

However, I am not sure whether to test this method - since it still opens streams and reads data.

+1
source

First understand the meaning of Mocking .

1) Why do we taunt?

Suppose we don’t want to call any original method and want instead of this original method we should call a dummy method, then we should look for ridicule.

For instance,

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

This means that whenever this execute() is called, you will return your own prepared value instead of the original mockHttpResponse .

So, in your case, prepare your stub object and scoff if you need it. Here you are preparing your answer (pay attention to the actual, but fictitious).

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

Therefore whenever you

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

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

When your control comes in

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

Then you will get {yourJsonString} when response.getEntity().getContent() is called, and let the break code execute its functionality. In the end, your finished JSON object will be prepared.

Remember that test cases are intended only to help developers. We can mock something and return something or any stub object. Just write a test case to test our control flow, passing expected and expected values.

This will simplify your work. Now you want to mock your BufferReader class.

  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")); 
0
source

All Articles