Contents

Setting the strictness of Mockito mock(s)

Introduction

Mockito has three levels of strictness, from none to strict stubbing, to help you write cleaner tests. The current default is to warn you about duplicate and unclear stubbing. Sometimes, you want a lower or higher strictness level for your test(s). In this post, we will look at how to set the strictness level for a class, and the strictness level for a single mock object.

Mockito's levels of strictness

These are the levels of strictness available in Mockito:

  • LENIENT
    • This is the Mockito 1 behavior and offers no extra strictness.
  • WARN
    • This is the default Mockito 2 and 3 behavior and print warnings to the console to help you improve your tests.
  • STRICT_STUBS
    • This is the highest level of strictness for Mockito and ensures that your tests are clean and with less duplication. This level of strictness is planned as the default for Mockito version 4.

Setting the strictness of a class with @MockitoSettings or @Rule

Setting the strictness level of an entire class when you use Junit5 and Mockito is done with the @MockitoSettings annotation. Mockito will use the strictness level you defined in the MockitoSettings for every mock created inside that class.

1
2
3
4
@MockitoSettings(strictness = Strictness.LENIENT)
public class MyTestClass{
    
}

Using JUnit4, you will have to use a @Rule instead of @MockitoSettings. A JUnit rule to change the strictness it looks like this:

1
@Rule public MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.LENIENT);

Setting the strictness with Mockito.mockitoSession()

If you want more control over the strictness of your mocks, you can use the MockitoSession. Mocks created during the session will use the strictness level you defined at the start. When the session finishes, your usage of the created mocks will be verified against the level of strictness you chose. It is essential to call mockito.finishMocking(); to end the session.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class MyTestClass{
    MockitoSession mockito;
    
    @BeforeEach
    void setup() {
        mockito = Mockito.mockitoSession()
                .strictness(Strictness.WARN)
                .startMocking();
    }
    
    @AfterEach
    void tearDown() {
        mockito.finishMocking();
    }
}

Setting the strictness for a single @Mock

It is also possible to change the strictness of a single mock. If you have been using @Mock before, you can change the strictness of the mock by setting it. @Mock(strictness = Strictness.LENIENT) tells Mockito to create a mock with lenient strictness.

1
2
@Mock(strictness = Strictness.LENIENT)
Car blueCar;

Setting the strictness for a mock(ClassToMock)

We can also change the strictness of the mocks you create yourself. Just as with @Mock you only have to pass the level of strictness you want when you create the mock. Like in the example below:

1
Car blueCar = Mockito.mock(Car.class, withSettings().strictness(Strictness.LENIENT));

Marking a method call as lenient

Mockito's when() call can also be marked with lenient level strictness. To do this, you need to use the when() from the lenient() method call.
The result will look like the example below.

1
lenient().when(blueCar.drive(10)).thenReturn("Driving at 10 kmh");

Conclusion

In this post, we looked at the different ways you can control the strictness of your mocks. We went over how to change the strictness of every mock in a class and the strictness of a single mock. It is also possible that Mockito version 4 will use STRICT_STUBS as default, so it is recommended to use STRICT_STUBS. Using STRICT_STUBS will help you write cleaner tests and have less duplication, but also that your tests won't fail when you upgrade to a future Mockito version with strict stubbing as the default.

Further reading

More about testing in Java:

Have a question or idea?

Thank you for reading my post. Have any questions about this topic or an idea for a new topic? Please reach out to me on Twitter or send me an email.

If you want to read more about Java when I write about it – you can follow me on Twitter.