293

Is there a way to verify if a methodOne is called before methodTwo in Mockito?

public class ServiceClassA {
    public void methodOne(){}
 }

public class ServiceClassB {
    public void methodTwo(){}
 }

public class TestClass {
    public void method(){
        ServiceClassA serviceA = new ServiceClassA();
        ServiceClassB serviceB = new ServiceClassB();
        serviceA.methodOne();
        serviceB.methodTwo();
    }
}

6 Answers 6

438

InOrder helps you to do that.

ServiceClassA firstMock = mock(ServiceClassA.class);
ServiceClassB secondMock = mock(ServiceClassB.class);

Mockito.doNothing().when(firstMock).methodOne();   
Mockito.doNothing().when(secondMock).methodTwo();  

//create inOrder object passing any mocks that need to be verified in order
InOrder inOrder = inOrder(firstMock, secondMock);

//following will make sure that firstMock was called before secondMock
inOrder.verify(firstMock).methodOne();
inOrder.verify(secondMock).methodTwo();
6
  • 12
    This is correct, though the calls to doNothing are not needed here except as a placeholder for other stubbing. Mockito will silently accept void method calls by default. Commented Feb 20, 2014 at 10:34
  • 4
    It accept them while the object have no dependencies if the object have dependencies there will be an exception =)
    – Koitoer
    Commented Feb 20, 2014 at 17:12
  • 29
    consider inOrder.verifyNoMoreInteractions(); after the last verify in this example to verify that no other calls were made.
    – DwB
    Commented Jun 30, 2016 at 19:02
  • 3
    Just to clarify: It is safe to define inOrder just before verify - after invoking some (tested) methods on mocks. Commented Aug 16, 2017 at 16:33
  • 1
    Are the results the same for inOrder(firstMock, secondMock) and inOrder(secondMock, firstMock)? Perhaps you can update the answer to make a note about this.
    – kevinarpe
    Commented Dec 27, 2017 at 10:49
144

Note that you can also use the InOrder class to verify that various methods are called in order on a single mock, not just on two or more mocks.

Suppose I have two classes Foo and Bar:

public class Foo {
  public void first() {}
  public void second() {}
}

public class Bar {
  public void firstThenSecond(Foo foo) {
    foo.first();
    foo.second();
  }
}

I can then add a test class to test that Bar's firstThenSecond() method actually calls first(), then second(), and not second(), then first(). See the following test code:

public class BarTest {
  @Test
  public void testFirstThenSecond() {
    Bar bar = new Bar();
    Foo mockFoo = Mockito.mock(Foo.class);
    bar.firstThenSecond(mockFoo);

    InOrder orderVerifier = Mockito.inOrder(mockFoo);
    // These lines will PASS
    orderVerifier.verify(mockFoo).first();
    orderVerifier.verify(mockFoo).second();

    // These lines will FAIL
    // orderVerifier.verify(mockFoo).second();
    // orderVerifier.verify(mockFoo).first();
  }
}
9
  • 2
    This should have been a comment on the accepted answer, not a whole new answer.
    – ach
    Commented Nov 14, 2016 at 19:12
  • 30
    I disagree with your comment @ach The code sample helps, so a new answer makes sense.
    – Snekse
    Commented Apr 14, 2017 at 16:49
  • 4
    Is there a way to verify the same method is called twice, but the verify the order of the parameters passed in? e.g. First find('foo'), then find('bar')
    – Snekse
    Commented Apr 14, 2017 at 16:51
  • 1
    Looks like this might be my answer stackoverflow.com/questions/36573399/…
    – Snekse
    Commented Apr 14, 2017 at 17:03
  • 8
    This is actually a better example than the accepted answer because it shows a more typical usage than doNothing() Commented May 1, 2019 at 15:06
44

Yes, this is described in the documentation. You have to use the InOrder class.

Example (assuming two mocks already created):

InOrder inOrder = inOrder(serviceAMock, serviceBMock);

inOrder.verify(serviceAMock).methodOne();
inOrder.verify(serviceBMock).methodTwo();
0
7

For Kotlin users, you can go this way:

class MyTrackerTest {
    private val trackEventUseCase: TrackEventUseCase = mock()
    private val sut = MyTracker(trackEventUseCase)

    @Test
    fun `trackSomething SHOULD invoke tracker use case twice with correct event names WHEN called`() {
        sut.trackSomething()

        trackEventUseCase.inOrder {
            verify().invoke("Is it August?")
            verify().invoke("No!")
        }
    }

}

2

With BDD it's

@Test
public void testOrderWithBDD() {


    // Given
    ServiceClassA firstMock = mock(ServiceClassA.class);
    ServiceClassB secondMock = mock(ServiceClassB.class);

    //create inOrder object passing any mocks that need to be verified in order
    InOrder inOrder = inOrder(firstMock, secondMock);

    willDoNothing().given(firstMock).methodOne();
    willDoNothing().given(secondMock).methodTwo();

    // When
    firstMock.methodOne();
    secondMock.methodTwo();

    // Then
    then(firstMock).should(inOrder).methodOne();
    then(secondMock).should(inOrder).methodTwo();


}
1
  • 1
    wthat is willDoNothing() doing there? Where is the reference? Why is it preceeding? Commented Jun 7, 2022 at 5:07
0

When verifying the invocation order of a single method call with different arguments it's essentially the same:

ServiceClass myMock = mock(ServiceClassA.class);

InOrder inOrder = inOrder(myMock);

inOrder.when(myMock).myMethod(42);
inOrder.when(myMock).myMethod(69);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.