I have a case where the code under test will make consecutive calls to
the same method with the same arguments and I need to verify the order
of the calls. I'm finding that Mockito is a bit of a pain in that
regard.
Consider this example. In the real example the order of calls and
values is determined by what is passed to the class under test:
interface Foo { void foo( int i ); };
@Test public void shouldBeAbleToSpecifyOrderOfRepeatedCalls()
{
Foo f = mock( Foo.class );
// Use mock
f.foo( 1 );
f.foo( 0 );
f.foo( 0 );
f.foo( 1 );
InOrder inOrder = inOrder( f );
inOrder.verify( f ).foo( 1 );
inOrder.verify( f ).foo( 0 );
inOrder.verify( f ).foo( 0 );
inOrder.verify( f ).foo( 1 );
}
That seems like a natural way to express this, but unfortunately this
test will fail. It complains when verifying the call with zero. It
says that it was expected once, but it occurred twice. Yes, it did
occur twice but I want to verify each individually.
The only way I can find to get this test to do what I want is:
inOrder.verify( f ).foo( 1 );
inOrder.verify( f, times( 2 ) ).foo( 0 );
inOrder.verify( f ).foo( 1 );
But that is often inconvenient and in many cases is less clear. The
fact that the calls are the same may be mere coincidence. Sometimes my
verification uses code to create the expectations. I often in addition
to testing with fixed inputs will generate random inputs and
algorithmically set the expectations based on the input. The exact
order is not necessarily known at compile time so having to use times
is very inconvenient.
--
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.