verifyNoMoreInteractions() trap

20 views
Skip to first unread message

szczepiq

unread,
Jul 6, 2008, 4:44:25 PM7/6/08
to moc...@googlegroups.com
I'll just reiterate what I commented on Ola's blog
(http://ola-bini.blogspot.com/2008/07/java-and-mocking.html) because I
believe some users might fall into verifyNoMoreInteractions() trap.

verifyNoMoreInteractions() is not a framework contract that you have
to remember and write for every test method.
verifyNoMoreInteractions() is just an assertion from the interaction
testing toolkit. Use it only if it is relevant. Have a look at this
dummy example:

someMethod() {
service.foo();
service.bar();
}

For above code I wouldn't use verifyNoMoreInteractions().

However in following scenario:

someMethod() {
if (condition) {
service.foo();
} else {
service.bar();
service.baz();
}
}

it perfectly makes sense to do the following:

shouldFooWhenCondition() {
...
verify(service).foo();
verifyNoMoreInteractions(service);
}

I use verifyNoMoreInteractions() only if it describes relevant application code.

Cheers,
Szczepan Faber

Igor Czechowski

unread,
Jul 7, 2008, 8:49:51 AM7/7/08
to moc...@googlegroups.com
I would make a test more explicit

shouldFooWhenCondition() {
...
verify(service).foo();

verify(service, never()).bar();
verify(service, never()).baz();
}

However, when there's more than a few undesirable interaction,
verifyNoMoreInteractions() is much more convenient.

Igor

Reply all
Reply to author
Forward
0 new messages