First time with mockito, couple specific questions

4,491 views
Skip to first unread message

phil swenson

unread,
Jul 22, 2010, 11:32:27 PM7/22/10
to mockito
Note this is my first Mockito test ever and I probably picked kind of
a hard one...

Also note that "invoker" is a mock.

I am looking to have a call to "invoker.invoke" match to the fourth
parameter being any instance of IData. IData doesn't override equals
(legacy code), so I haven't been able to get mockito to recognize any
specific IData instance. So I am trying to get it to match anything
as the first 3 params should be specific enough.

when(invoker.invoke(eq(USER), eq(PASSWORD), eq("wm.server.ns"),
eq("nodeExists"), (IData)any())).thenReturn(resultData);

But this doesn't appear to work, it returns null.


2nd question:

I gather "verify" is the actual "test".

I want to test that inside this method:
jtc.createTrigger(BROKER_ALIAS, TOPIC_NAME, DEST_TYPE, PACKAGE_NAME,
TRIGGER_NAME);

this method is called with the expected "data" (IData again)
invoker.invoke(USER, PASSWORD, "wm.server.jms", "createJMSTrigger",
data);

And note that I can't call equals() on the "data". But I can inspect
it via methods.

thoughts?

szczepiq

unread,
Jul 23, 2010, 10:21:30 AM7/23/10
to moc...@googlegroups.com
Hey,

#1 check if other args are really matching, check if you are calling the right method (overloading), check if the method isn't final by any chance,

#2 Have a look at documentation of ArgumentCaptor. Might be useful for your case.

Cheers,
Szczepan


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


phil swenson

unread,
Jul 23, 2010, 5:15:20 PM7/23/10
to moc...@googlegroups.com
So got #1 solved, you were right no match. Here is a little more detail on #2:

inside this method:
jtc.createTrigger(BROKER_ALIAS, TOPIC_NAME, DEST_TYPE, PACKAGE_NAME,
TRIGGER_NAME);

a call is made to a mocked "invoker" like so:
mockedInvoker.invoke(USER, PASSWORD, "wm.server.jms", "createJMSTrigger", data);

I'm interested in verifying this call is made and that the "data" is
correct. Unfortunately, the person who wrote the data class (which is
of type IData) didn't override equals. So what I need to do is
capture the value that is passed and manually inspect it.

So I tried this:

ArgumentCaptor<IData> argument = ArgumentCaptor.forClass(IData.class);
verify(invoker.invoke(eq(USER), eq(PASSWORD), eq("wm.server.jms"),
eq("createJMSTrigger"), argument.capture()));

But I get this error:
org.mockito.exceptions.misusing.NullInsteadOfMockException:
Argument passed to verify() should be a mock but is null!
Examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();

note that I verified "invoker" is not null (it is mocked). And
"argument" is not null.

ideas?

Chris Bartling

unread,
Jul 23, 2010, 5:22:43 PM7/23/10
to moc...@googlegroups.com
Your verify is wrong.  It should be...

verify(invoker).invoke(eq(USER), eq(PASSWORD), eq("wm.server.jms"),
eq("createJMSTrigger"), argument.capture());

This has caught me more than once.  Common mistake.

-- chris -- 

phil swenson

unread,
Jul 23, 2010, 5:32:02 PM7/23/10
to moc...@googlegroups.com
thanks, that did it!
Reply all
Reply to author
Forward
0 new messages