Trouble returning IOException from BufferedReader readLine call.

92 views
Skip to first unread message

Timothy Wasson

unread,
Feb 10, 2012, 12:18:32 PM2/10/12
to PowerMock
I've only been using PowerMock for a few days now and I've managed to
get through just any every case I've encountered. I'm writing unit
tests against some legacy code.

Here's the problem

Before I even mock out the BufferedReader, I mocked out the inline
response entity inside of the BufferedReader constructor to return the
String "Fake Entity".

e.g.
PowerMockito.when(response.getEntity()).thenReturn("Fake Entity");

Why ? Because It gets evaluated before the constructor and if I don't
mock it out I get a NullPointerException. So no problems there.....

The problem though is that I am attempting to mock out a readLine call
and have it throw an IOException.However, instead it of throwing the
exception the result of the read is always "Fake Entity". It's not
getting mocked. Dunno why.

Here's the code snippet.I'm trying to test.

BufferedReader br = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(
response.getEntity().getBytes())));
String output;

try {
if ((output = br.readLine()) != null) {
...
}
} catch (IOException e1) {
//do something here
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ioe) {
}

}
}

@Test
public void testSendWhenBufferedReaderReadLineIOException()
throws Exception {

ClientResponse<String> response = PowerMockito
.mock(ClientResponse.class);
PowerMockito.when(response.getEntity()).thenReturn("Fake Entity");
BufferedReader bReader = PowerMockito.mock(BufferedReader.class);
PowerMockito.when(bReader.readLine()).thenThrow(new IOException("Fake
IOException"));

}

I've even tried mocking out all the individual constructors in the
inline (e.g. using whenNew), but that makes no difference.

Any clue what might be going on here?

Johan Haleby

unread,
Feb 11, 2012, 6:24:53 AM2/11/12
to powe...@googlegroups.com
It's probably because BufferedReader is a java system class so you need to deal with it in a special way.

How ever I would be really careful with mocking classes such as BufferedReader. I would rather write an integration test to verify the use case and then refactor your legacy code, step by step.

Regards,
/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Timothy Wasson

unread,
Feb 12, 2012, 6:31:55 PM2/12/12
to PowerMock
Thanks Johan!... in the end I just did the refactor.

On Feb 11, 7:24 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> It's probably because BufferedReader is a java system class so you need to
> deal with it in a special
> way.<http://code.google.com/p/powermock/wiki/MockSystem>
>
> How ever I would be really careful with mocking classes such as
> BufferedReader. I would rather write an integration test to verify the use
> case and then refactor your legacy code, step by step.
>
> Regards,
> /Johan
>
Reply all
Reply to author
Forward
0 new messages