How to mock java.io exceptions?

1,761 views
Skip to first unread message

peakdetector

unread,
Jul 31, 2009, 12:10:19 AM7/31/09
to PowerMock
I am trying to mock java.io.FileNotFoundException and
java.io.IOException
in the below function.

class Util {
public static String readFileIntoBuffer(final String filename) {
StringBuffer buffer = new StringBuffer();
BufferedReader in = null;
FileReader f = null;
try {
f = new FileReader(filename);
in = new BufferedReader(f);

String str;
while((str = in.readLine()) != null) {
buffer.append(str);
}
f.close();
f= null;
in.close();
in = null;
}catch (FileNotFoundException e)
{
log.severe("FileNotFoundException:Failed to read file:" +
filename );


}
catch (IOException e)
{
log.severe("IOException:Failed to read file:" +
filename );

}
finally
{
try
{
if(f != null)
f.close();
if(in != null)
in.close();
}
catch (Exception ex)
{
}


}
return buffer.toString();
}
}

Unit test case:
@Test
public void testreadFileIntoBuffer_FileNotFoundException() throws
Exception {
System.out.println
("getreadFileIntoBuffer_FileNotFoundException");
FileReader f = PowerMock.createMock(FileReader.class);
PowerMock.expectNew(FileReader.class, Config.MAF_MSG).andThrow
(new
java.io.FileNotFoundException("FileNotFoundException"));
PowerMock.replay(f, FileReader.class);
PowerMock.expectNew(BufferedReader.class, f).andThrow(new
java.io.FileNotFoundException("FileNotFoundException"));
PowerMock.replay(BufferedReader.class);

String result =
Util.readFileIntoBuffer(Config.MAF_MSG);
assertEquals("", result);
PowerMock.verify(FileReader.class);
}

What I am missing?

Johan Haleby

unread,
Jul 31, 2009, 2:15:29 AM7/31/09
to powe...@googlegroups.com
Hi,

Have you prepared Util.class for test using the @PrepareForTest annotation? Also you don't need to expectNew for the BufferedReader class because an exception would already have been throwed before. The test is also wrong since you need to expect the exception as the outcome of your test, i.e. you should declare you test-method with @Test(expected=FileNotFoundException.class). You may also use replayAll() and verifyAll() to make the test a bit simpler.

/Johan

Johan Haleby

unread,
Jul 31, 2009, 2:20:59 AM7/31/09
to powe...@googlegroups.com
Sorry, ignore the thing about "@Test(expected=FileNotFoundException.class)", I didn't read your code thoroughly enough. One more thing that you may have forgot is to run the test with the PowerMockRunner (i.e. @RunWith(PowerMockRunner.class)).

/Johan

peakdetector

unread,
Jul 31, 2009, 10:05:44 AM7/31/09
to PowerMock
Thanks for quick response. I was missing @PrepareForTest for Util
class. I misunderstood the concept. I thought @PrepareForTest is
used for class which is being mocked so I had @PrepareForTest
({FileReader.class, BufferedReader.class})

Thanks again and you guys rock !!



On Jul 31, 2:20 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Sorry, ignore the thing about "@Test(expected=FileNotFoundException.class)",
> I didn't read your code thoroughly enough. One more thing that you may have
> forgot is to run the test with the PowerMockRunner (i.e.
> @RunWith(PowerMockRunner.class)).
>
> /Johan
>
> On Fri, Jul 31, 2009 at 8:15 AM, Johan Haleby <johan.hal...@gmail.com>wrote:
>
> > Hi,
>
> > Have you prepared Util.class for test using the @PrepareForTest annotation?
> > Also you don't need to expectNew for the BufferedReader class because an
> > exception would already have been throwed before. The test is also wrong
> > since you need to expect the exception as the outcome of your test, i.e. you
> > should declare you test-method with
> > @Test(expected=FileNotFoundException.class). You may also use replayAll()
> > and verifyAll() to make the test a bit simpler.
>
> > /Johan
>
Reply all
Reply to author
Forward
0 new messages