Verifying two invocations of the same method with another method invocation in between, when order is important, in Mockito

java, mockito

Solution

It tested this with Mockito 1.9.5 and it works:

@Test
public void foo() {
    Runnable outer = Mockito.mock(Runnable.class, "outer");
    Runnable inner = Mockito.mock(Runnable.class, "inner");

    outer.run();
    inner.run();
    outer.run();

    InOrder order = Mockito.inOrder(outer, inner);
    order.verify(outer).run();
    order.verify(inner).run();
    order.verify(outer).run();
}

So if you aren't doing anything else wrong your code should work. What Mockito version are you using?

Problem

I thought this would work: ``` InOrder inOrder = new InOrder(mock); inOrder.verify(mock).method1(); inOrder.verify(mock).method2(); inOrder.verify(mock).method1(); ``` … but Mockito says `undesired invocation of mock.method1(). Wanted 1 time, but was 2 times.` I changed my code to this: ``` inOrder.verify(times(2), mock).method1(); inOrder.verify(mock).method2(); ``` It should work, but now I do not test what I wanted to test in the first place. Could someone please point out what I am doing wrong, or if Mockito is too limited for these kind of test?

Original source