Getting "Wanted but not invoked" exceptions using spy(..) if methods invoked before spy is created.

1,017 views
Skip to first unread message

Andrew

unread,
Oct 12, 2010, 2:52:36 AM10/12/10
to mockito
Hi there,

I'm not sure if I'm going crazy but I'm getting "Wanted but not
invoked:" errors when using verify(..) on a spy().

In my scenario I'm testing my AbstractListEditController by spying on
a dummy implementation. The dummy implementation is as follows.

// My test class
private class TestController extends
AbstractListEditController<String>
{
private TestController() {
}

private TestController(MutableListModel<String>
stringMutableListModel) {
super(stringMutableListModel);
}

// this is the abstract method I'm ensure get's called in the
test.
protected void synchroniseUi() {
// if I put System.out.println("Called") it gets printed
out as expected.
}

// I've omitted other methods here that don't get called
}

If I create my test method as follows I get the "Wanted but not
invoked" error.

@Test
public void ensureModelChangesCallSynchroniseUi() {
ArrayListModel<String> model = new ArrayListModel<String>();

// create the spy passing the model, synchroniseUi will be
called during construction.
AbstractListEditController<String> dut = spy(new
TestController(model));
model.add("testing...");
verify(dut, times(1)).synchroniseUi();
}

However if I do the following it does work;


@Test
public void ensureModelChangesCallSynchroniseUi() {
ArrayListModel<String> model = new ArrayListModel<String>();

// create the spy passing without the model so synchroniseUi
isn't called.
AbstractListEditController<String> dut = spy(new
TestController());
// now set the model
dut.setModel(model);

// and do the test...
model.add("testing...");

// but I have to check for 2 calls.
verify(dut, times(2)).synchroniseUi();
}

But I'd much rather use the previous test method rather than have to
account for the constructor call to synchroniseUi().

I'm using mockito 1.8.5 and testng 5.8.

Thanks for any help
Cheers
Andrew

szczepiq

unread,
Oct 12, 2010, 3:59:55 AM10/12/10
to moc...@googlegroups.com
Hey,

You've got following options:

1. Account for extra calls. Extra calls do happen so why would I want to hide it?
2. Use atLeastOnce() verification
3. Reset the spy after creation
4. Try to write the code so that spies of real objects are not needed :-) (my favorite strategy).

Now it is up to you ;)
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.


Rick

unread,
Oct 12, 2010, 5:29:11 AM10/12/10
to mockito
Andrew,

This would help explain the behavior you are observing. If you break
up the compound statement

AbstractListEditController<String> dut = spy(new
TestController(model));

into two

AbstractListEditController<String> dut = new TestController(model);
dut = spy(new TestController(model));

You will see that TestController is fully constructed before passing
to the spy() method. As such, none of the interactions that happens
during construction of the TestController (and its super() method
call) are recorded by the spy().

--
Rick

szczepiq

unread,
Oct 12, 2010, 5:44:09 AM10/12/10
to moc...@googlegroups.com
>If you break up the compound statement

then nothing really changes :) Java evaluates the parameters before spy() method gets called anyway.

Hope that helps,
Szczepan

Andrew

unread,
Oct 12, 2010, 11:51:25 PM10/12/10
to mockito
I think this is why I was so confused. i.e. Why would the constructor
invoking the method in question cause the spy to fail? Very
confusing.

Thanks for the hints. I have reasons for the class in question being
abstract, they may not be good but for now it's what I have to work
with. I'll probably live with the extra events for now (I read that
reseting the spy is heartily discouraged).

Thanks for your help,
Cheers
Andrew
> > mockito+u...@googlegroups.com<mockito%2Bunsu...@googlegroups.com >
> > .
Reply all
Reply to author
Forward
0 new messages