Contents

Verify method calls with Mockito

Writen by: David Vlijmincx

Introduction

In this post, I will show how you can use Mockito verify to check that a method has or hasn't been called. The Mockito verify method enables you to verify that a method is never called or called any number of times, and even the order they are called in.

Verify a method is called n times

To verify that a method is called you can use the verify method. The verify method accepts a mock and times instance. The time instance is used to specify how often a method is supposed to be called. In the following example, mockito checks that a mock is called exactly two times.

1
2
3
4
5
6
7
8
9
@Test
void mockitoVerifyExample(){
   Car car = mock(Car.class);

   car.startEngine();
   car.startEngine();

   verify(car, times(2)).startEngine();
}

When using times(2) mockito knows that the method has to be called twice.

Exactly once

If you want to verify that a method is only called once you don't have to use a times(1) instance. Mockito will assume that you only want to have the method to be invoked once if you do not pass a times instance.

1
2
3
4
5
6
7
8
@Test
void mockitoVerifyExample(){
   Car car = mock(Car.class);

   car.startEngine();

   verify(car).startEngine();
}

Without the time instance verify(car).startEngine(); and verify(car, times(1)).startEngine(); have the same result

Verify a method called between boundaries

If you only that a method is supposed to be called at least or at most x times. you can use atMost and atLeat instead of times. In the following example, Mockito verifies that a method is called between 2 and 3 times.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@Test
void mockitoVerifyExample(){
   Car car = mock(Car.class);

   car.startEngine();
   car.startEngine();
   car.startEngine();

   verify(car, atLeast(2)).startEngine();
   verify(car, atMost(5)).startEngine();

}

Using both atLeast and atMost creates a boundary. You can also only use one of them to create a minimum or maximum number of times a method is supposed to be called.

Called between 1 and 0 times or more than once

Using verify you can also verify that a method is called between 0 and 1 times using atMostOnce(). Or you can use atLeastOnce if you only want to know a method is called at least once. In the following example, both methods are used to verify that the method has been called.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void mockitoVerifyExample(){
   Car car = mock(Car.class);

   car.startEngine();

   verify(car, atMostOnce()).startEngine();
   
   verify(car, atLeastOnce()).startEngine();

}

Verify a method is not called

You can also verify that the method has never been called by using never().

1
2
3
4
5
6
@Test
void mockitoVerifyExample(){
    Car car = mock(Car.class);
    
    verify(car, never()).startEngine();
}

No interaction with a mock

If you do not want any interaction with your mock you can use verifyNoInteractions().

1
2
3
4
5
6
@Test
void mockitoVerifyExample(){
   Car car = mock(Car.class);

   verifyNoInteractions(car);
}

Verify order of method calls

You can use InOrder if the order of the method calls is important for your tests. You can pass one or more mock instances to the InOrder method. In the following example, the InOrder instance is used to verify that the methods have been called in the correct order.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@Test
void mockitoVerifyExample(){
    Car car = mock(Car.class);

    car.setBrandName("Audi");
    car.startEngine();

    InOrder inOrder = inOrder(car);
    inOrder.verify(car).setBrandName("Audi");
    inOrder.verify(car).startEngine();

}

This example showed how to verify the order of method calls with one mock, but you can also use multiple mock instances as is shown in the next section.

Verify order with multiple mocks

In the following example, the order of multiple mock instances and method calls is verified. Using the InOrder instance allows us to specify in which order which method from each mock has to be called.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
@Test
void mockitoVerifyExample(){
    Car audi = mock(Car.class);
    Car bmw = mock(Car.class);


    audi.setBrandName("Audi");
    bmw.setBrandName("bmw");
    audi.startEngine();
    bmw.startEngine();

    InOrder inOrder = inOrder(audi,bmw);

    inOrder.verify(audi).setBrandName("Audi");
    inOrder.verify(bmw).setBrandName("bmw");

    inOrder.verify(audi).startEngine();
    inOrder.verify(bmw).startEngine();
}

InOrder works for one or multiple mock instances.

Verify with timeout

Using both verify and timeout allows you to test concurrent code. Using timeout is not recommended as there are better ways to test your concurrent code. Know that exists, but it's not supposed to be used often.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@Test
void mockitoVerifyExample(){
    Car car = mock(Car.class);

    car.startEngine();

    verify(car, timeout(1000)).startEngine();
    verify(car, timeout(1000).times(1)).startEngine();
    verify(car, timeout(1000).atLeast(1)).startEngine();
    verify(car, timeout(1000).atLeastOnce()).startEngine();
}

The timeout parameter has an int as parameter which is the total timeout in milliseconds.

Conclusion

In this post, we looked at all the different ways you can verify the behavior of your mock or spy instances. As you have seen there are multiple methods mockito provides somewhat of the same functionality. Because they somewhat overlap with each other it is good to make an informed decision on which verify method to use. If you want to know more about mockito's verification methods please take a look at the official documentation.

 




Questions, comments, concerns?

Have a question or comment about the content? Feel free to reach out!