Null pointers from a mock object? Newbie question.

19,923 views
Skip to first unread message

Peter

unread,
Jul 8, 2010, 9:09:55 AM7/8/10
to mockito
I'm getting null pointers when trying to create a mock object. I'm a
little confused as I thought the whole point was that the actual code
of the mocked class would not be executed.

However I'm trying to mock a net.sf.ehcacheCache object

Cache cache = mock(Cache.class);
when(cache.get(anyObject())).thenReturn(null);

I get a null pointer exception when cache.get tries to access the
private variable 'status' which is null.

This seems so basic that I must be doing something foolish.

Thanks,
Peter

Max

unread,
Jul 8, 2010, 9:30:30 AM7/8/10
to moc...@googlegroups.com
Use doReturn(null).when(cache).get(anyObject());

Also since you're mocking Cache it should return null by default. You don't have to stub it.


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


Peter

unread,
Jul 8, 2010, 10:26:03 AM7/8/10
to mockito
I'm getting the same error. The real code is.

Limit cachedLimit = mock(Limit.class);

String cacheKey = "limexCode:2010-07-22:false";
Element cacheElement = new Element(cacheKey, cachedLimit);

Cache cache = mock(Cache.class);
doReturn(cacheElement).when(cache).get(Matchers.<Serializable>anyObject())

I used null in my previous example to show prove that the error wasn't
caused by the cacheElement.

The issue is that the real get method code is being called.

Thanks,
Peter


On Jul 8, 2:30 pm, Max <maxon...@gmail.com> wrote:
> Use doReturn(null).when(cache).get(anyObject());
>
> Also since you're mocking Cache it should return null by default. You don't
> have to stub it.
>
> On Thu, Jul 8, 2010 at 2:09 PM, Peter <proggerp...@gmail.com> wrote:
> > I'm getting null pointers when trying to create a mock object. I'm a
> > little confused as I thought the whole point was that the actual code
> > of the mocked class would not be executed.
>
> > However I'm trying to mock a net.sf.ehcacheCache object
>
> > Cache cache = mock(Cache.class);
> > when(cache.get(anyObject())).thenReturn(null);
>
> > I get a null pointer exception when cache.get tries to access the
> > private variable 'status' which is null.
>
> > This seems so basic that I must be doing something foolish.
>
> > Thanks,
> > Peter
>
> > --
> > 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>
> > .

Peter

unread,
Jul 8, 2010, 10:30:03 AM7/8/10
to mockito
Ah, it seems the get method is final...

Is there any way around this?

szczepiq

unread,
Jul 8, 2010, 10:30:12 AM7/8/10
to moc...@googlegroups.com
Most likely method you're mocking is final.

Cheers,
Szczepan
PS.
Final methods suck
Final classes suck
private constructors suck because they make classes implicitly final
(I had to say it for some unknown reason...)

To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages