set different answers with "when" and "doAnswer" for different parameters but Method answer is allways called

1,751 views
Skip to first unread message

ich du

unread,
Jun 12, 2012, 7:40:55 AM6/12/12
to moc...@googlegroups.com
i set up a mock with the following 2 answers (it's gwt related):

        when(hMapper.getToken(any(DynamicTablePlace.class))).thenReturn(DYNAMIC_CONTENT);
        
        doAnswer(new Answer<Object>() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                NewNavigationPlace place = (NewNavigationPlace) invocation.getArguments()[0];
                return place.getAgsToken() + "," + place.getTopicToken() + "," + place.getDownloadOption();
            }
        }).when(hMapper).getToken(any(NewNavigationPlace.class));

The Problem is that the answer method is not only be called for "NewNavigationPlace" but also for "DynamicTablePlace" but in the latter case " invocation.getArguments()[0]" is null and so i get a null pointer exception. 
Is this working as intended? What i did wrong? 

thx in advance

Eric Lefevre-Ardant

unread,
Jun 12, 2012, 8:29:54 AM6/12/12
to moc...@googlegroups.com
Is NewNavigationPlace a parent for DynamicTablePlace? In that case, it seems to me that your doAnswer is overriding your first line of code.
TBH, I'm not sure how Mockito works regarding the any(Class) clause. It could be that it overrides all previous configuration for that method.


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/xbk4UbsZZrUJ.
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.

ich du

unread,
Jun 12, 2012, 9:11:24 AM6/12/12
to moc...@googlegroups.com
both places are children of "Place"

it seems that "answer" is called regardless of parameter type. i edited the answer:

            public Object answer(InvocationOnMock invocation) throws Throwable {
                if (invocation.getArguments()[0] instanceof NewNavigationPlace) {
                    NewNavigationPlace place = (NewNavigationPlace) invocation.getArguments()[0];
                    return place.getAgsToken() + "," + place.getTopicToken() + "," + place.getDownloadOption();
                }
                if (invocation.getArguments()[0] instanceof DynamicTablePlace)
                    return DYNAMIC_CONTENT;
                return null;
            }
    }).when(hMapper).getToken(any(NewNavigationPlace.class));

This is working! -> "DYNAMIC_CONTENT" is returned. So the answer is called also for DynamicTablePlace and it is also called for null (source of NullPointerException).
Is this a bug?
the behavior is the same if ... .getToken(any(Place.class)); is set.
To unsubscribe from this group, send email to mockito+unsubscribe@googlegroups.com.

Brice Dutheil

unread,
Jun 22, 2012, 5:45:57 AM6/22/12
to moc...@googlegroups.com
Hi,

Yes Eric is correct, the second stubs overide the first one as they are both equivalent : they both use any.

We made that a bit more cleared in the javadoc, any(Class) is just here to avoid casts in your code.

For now if you want to check arguments based on types, then you should use isA(Class)


Cheers,
-- Brice



To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/8X6_txmyMGsJ.

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