How do I mock the contents of a file?

11,810 views
Skip to first unread message

W.P. McNeill

unread,
Sep 16, 2010, 7:48:20 PM9/16/10
to mockito
I am writing unit tests for a component that reads information from a
configuration file. I want to put the contents of that file in the
test source using Mockito rather than having an actual file in the
file system.

Specifically I have a program that does this:

BufferedReader reader = new BufferedReader(new
FileReader(filename));
try {
String line;
while (null != (line = reader.readLine())) {
...do stuff with line...
}
} finally {
reader.close();
}

and somewhere in my unit test code I want a string like
"line1\nline2\nline3".

Is there an easy way to do this with Mockito? Is this the right
approach to unit testing programs that read from files?

Malte Finsterwalder

unread,
Sep 17, 2010, 3:47:27 AM9/17/10
to moc...@googlegroups.com
I don't know of any Mockito-Tricks.

I usually use one of two approaches to test with files:

1.) I actually write the file in the setup of the test.

2.) My code uses a Reader and I hand in a StringReader.

But the majority of my tests usually mock out the whole
component/class, that does the filehandling, since that is usually
easier and more precise.

Greetings,
Malte

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

szczepiq

unread,
Sep 17, 2010, 8:37:04 AM9/17/10
to moc...@googlegroups.com
I completely agree with Malte!

Cheers,
Szczepan

James Carr

unread,
Sep 17, 2010, 9:20:31 AM9/17/10
to moc...@googlegroups.com
My approach is that if I have to use external pieces to the system
I'll either use a reader like you mention or I'll write an integration
test against the piece that must hit some external resource. The the
object that performs the work with that external resource can then be
susbstituted in tests. :)

Thanks,
James

Robert Zaleski

unread,
Sep 18, 2010, 8:47:12 AM9/18/10
to mockito
Since fast unit tests shouldn't touch the hard disk, I separate the
code using the reader I want to test from the code that initializes
the reader (Opens the file)

Using a buffered reader has actually been pretty painless for me to
unit test I typically do

BufferedReader rdr = mock(BufferedReader.class);
when(rdr.readLine()).thenReturn(
"Line 1 of some file",
"Line 2 of some file",
"Line 3 of some file",
null
);

That behaves how I expect, and I can set up various scenarios for
snippets of files I'm reading pretty easily using this method, and run
them all in under a second without actually reading the code, which
was the point of a unit test in the first place. Passing a
BufferedReader wrapping a StringReader is an interesting approach
though, and may make setting up tests a bit faster than breaking them
into Strings like I currently do.

Robert

On Sep 17, 9:20 am, James Carr <james.r.c...@gmail.com> wrote:
> My approach is that if I have to use external pieces to the system
> I'll either use a reader like you mention or I'll write an integration
> test against the piece that must hit some external resource. The the
> object that performs the work with that external resource can then  be
> susbstituted in tests. :)
>
> Thanks,
> James
>
> On Fri, Sep 17, 2010 at 2:47 AM, Malte Finsterwalder
>
> <ma...@finsterwalder.name> wrote:
> > I don't know of any Mockito-Tricks.
>
> > I usually use one of two approaches to test with files:
>
> > 1.) I actually write the file in the setup of the test.
>
> > 2.) My code uses a Reader and I hand in a StringReader.
>
> > But the majority of my tests usually mock out the whole
> > component/class, that does the filehandling, since that is usually
> > easier and more precise.
>
> > Greetings,
> >   Malte
>
> > On 17 September 2010 01:48, W.P. McNeill <bill...@gmail.com> wrote:
> >> I am writing unit tests for a component that reads information from a
> >> configuration file.  I want to put the contents of that file in the
> >> test source using Mockito rather than having an actual file in the
> >> file system.
>
> >> Specifically I have a program that does this:
>
> >>                BufferedReader reader = new BufferedReader(new
> >> FileReader(filename));
> >>                try {
> >>                        String line;
> >>                        while (null != (line = reader.readLine())) {
> >>                                ...do stuff with line...
> >>                        }
> >>                } finally {
> >>                        reader.close();
> >>                }
>
> >> and somewhere in my unit test code I want a string like
> >> "line1\nline2\nline3".
>
> >> Is there an easy way to do this with Mockito?  Is this the right
> >> approach to unit testing programs that read from files?
>
> >> --
> >> You received this message because you are subscribed to the Google Groups "mockito" group.
> >> To post to this group, send email to moc...@googlegroups.com.
> >> To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
> >> For more options, visit this group athttp://groups.google.com/group/mockito?hl=en.

szczepiq

unread,
Sep 18, 2010, 11:47:43 AM9/18/10
to moc...@googlegroups.com
>BufferedReader rdr = mock(BufferedReader.class);

no, no, no! don't mock types you don't own...

>Passing a BufferedReader wrapping a StringReader is an interesting approach

yes! Making the reader injectable is one of the good solutions.

Cheers,
Szczepan
Reply all
Reply to author
Forward
0 new messages