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