[mockito] How to mock ObjectInputStream?

662 views
Skip to first unread message

Leo Jay

unread,
May 25, 2010, 6:43:06 AM5/25/10
to mockito
Hi All,

Following code throws NPE:
ObjectInputStream ois = mock(ObjectInputStream.class);
doReturn(3L).when(ois).readObject();
System.out.println(ois.readObject());

I tried to set the 2nd parameter of method "mock" to
RETURNS_SMART_NULLS, RETURNS_MOCKS, RETURNS_DEEP_STUBS, but the result
is the same.

I also tried to change the 2nd statement to:
when(ois.readObject()).thenReturn(3L);

It's still the same.

So, what's the correct way to mock the ObjectInputStream?

Thanks.

--
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.

Maciej Biłas

unread,
May 25, 2010, 1:38:37 PM5/25/10
to moc...@googlegroups.com
Hey Leo,

Mockito cannot mock final methods and readObject() has the final
modifier in ObjectInputStream class.

Please refer to:
http://code.google.com/p/mockito/wiki/FAQ

If you still want to mock final methods you could give PowerMock a try:
http://code.google.com/p/powermock/wiki/MockFinal

Best wishes,
Maciej
--
Maciej Biłas

szczepiq

unread,
May 25, 2010, 2:39:06 PM5/25/10
to moc...@googlegroups.com
Hey,


>So, what's the correct way to mock the ObjectInputStream?

Never mock ObjectInputStream. You don't own this type so you should not mock it. Forget about mocking framework, get creative and figure out a smart way of testing the code you're interested in (don't hestiate to refactor). Good luck! :D

Cheers,
Szczepan

Leo Jay

unread,
May 25, 2010, 11:25:49 PM5/25/10
to mockito
Hey,

Thanks.
I didn't realize readObject() is a final method.

> > For more options, visit this group athttp://groups.google.com/group/mockito?hl=en.

Leo Jay

unread,
May 25, 2010, 11:39:01 PM5/25/10
to mockito
Hey,

Thanks for you reply.
I have a method that reads some input data from an ObjectInputStream
and do
something. The first idea come up to my mind of testing this method is
to send
a mocked ObjectInputStream to the method.
If you don't think it's a good idea to mock ObjectInputStream, do you
have any
suggestions about how to test (or refactor) this?

Thanks.

BTW, do you think it would be good to show a warning or throw an
exception if
I'm mocking a final method, instead of invoking the real method
silently and
give me a misleading NPE?

On May 26, 2:39 am, szczepiq <szcze...@gmail.com> wrote:
> Hey,
>
> >So, what's the correct way to mock the ObjectInputStream?
>
> Never mock ObjectInputStream. You don't own this type so you should not mock
> it. Forget about mocking framework, get creative and figure out a smart way
> of testing the code you're interested in (don't hestiate to refactor). Good
> luck! :D
>
> Cheers,
> Szczepan
>
> On Tue, May 25, 2010 at 7:38 PM, Maciej Biłas <mac...@inszy.org> wrote:
> > Hey Leo,
>
> > Mockito cannot mock final methods and readObject() has the final
> > modifier in ObjectInputStream class.
>
> > Please refer to:
> >http://code.google.com/p/mockito/wiki/FAQ
>
> > If you still want to mock final methods you could give PowerMock a try:
> >http://code.google.com/p/powermock/wiki/MockFinal
>
> > Best wishes,
> > Maciej
>

> > On Tue, May 25, 2010 at 12:43 PM, Leo Jay <python.leo...@gmail.com> wrote:
> > > Hi All,
>
> > > Following code throws NPE:
> > >        ObjectInputStream ois = mock(ObjectInputStream.class);
> > >        doReturn(3L).when(ois).readObject();
> > >        System.out.println(ois.readObject());
>
> > > I tried to set the 2nd parameter of method "mock" to
> > > RETURNS_SMART_NULLS, RETURNS_MOCKS, RETURNS_DEEP_STUBS, but the result
> > > is the same.
>
> > > I also tried to change the 2nd statement to:
> > >        when(ois.readObject()).thenReturn(3L);
>
> > > It's still the same.
>
> > > So, what's the correct way to mock the ObjectInputStream?
>
> > > Thanks.
>
> > > --
> > > 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<mockito%2Bunsu...@googlegroups.com>


> > .
> > > For more options, visit this group at
> >http://groups.google.com/group/mockito?hl=en.
>
> > --
> > Maciej Biłas
>
> > --
> > 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<mockito%2Bunsu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages