[JAVA] Changes in Mockito 2

About the changes in Mockito 2. The content is for Mockito 1 users.

About release policy

Release policy changed was added on 2017/04. Previously it was released with every fix, but now only important versions (basically updated major / minor versions) will be provided by the JCenter / Maven central repository.

Changes from Mockito 1

The main changes and additional functions are described below. For more information, see Wiki, Release Notes /blob/release/2.x/doc/release-notes/official.md) and [Mockito Javadoc](https://javadoc.io/page/org.mockito/mockito-core/latest/org/mockito/ Please check Mockito.html # 0).

You can now mock final classes / methods

Mockito 1 required Power Mock, but from Mockito 2 it is possible to mock these by enabling the inline mock maker. .. The Kotlin class specified as final by default also uses kotlin-allopen plugin. You can mock without using it.

To enable this feature, prepare the following MockMaker files in the test resource directory.



Alternatively, specify the mockito-inline artifact instead of mockito-core.


dependencies {
    //testCompile "org.mockito:mockito-core:+"
    testCompile "org.mockito:mockito-inline:+"

However, this artifact is transient until the feature becomes the default and may be removed in the future.

This feature is provided by a mock maker called InlineByteBuddyMockMaker, but for this class [@Incubating](https://javadoc.io/page/org.mockito/mockito-core/latest/org/mockito/Incubating .html) Annotated. The @Incubating annotation is added to the new API and indicates that we expect user feedback. Please note that the API may change (although I think it is rare) depending on the feedback.

Note that static methods and constructor mocking are not supported, so if you want to mock them, you will still need PowerMock.

A library for Android devices has been prepared

Previously you needed Dexmaker, but Mockito 2.6 now provides a library for Android devices. You can use Mockito on your Android device by specifying mockito-android as a library of androidTestCompile scope.


dependencies {
    androidTestCompile "org.mockito:mockito-android:+"

mockito-android allows you to mock package private classes that was not possible with Dexmaker.

Note that you cannot mock final classes / methods with this artifact. (The inline mock maker mentioned above is also not compatible with Android) Regarding this, although it is my work, I provide a library called DexOpener that realizes mocking of final classes / methods on Android devices. Please try it if you like.

You can now write ArgumentMatchers in lambda expressions

In Mockito 1, ArgumentMatcher was an abstract class. Now that Mockito 2 has changed to a SAM type interface, you can use lambda expressions with ʻargThat (ArgumentMatcher) `.

any (Class) no longer matches null

ʻAny ()and ʻany (Class)are now distinctly different methods. ʻAny ()matches any argument, including null, but ʻany (Class)does not match null.

Also, the class in which these methods are defined has been changed from Matchers to ArgumentMatchers.

The anyXxx () system no longer matches null

AnyXxx () matchers such as ʻanyString ()and ʻanyList ()no longer match null.

anyObject () / anyVararg () has been deprecated

Mockito 2 uses ʻany () `.

Add nullable (Class)

Nullable (Class) has been added as a method very similar to ʻisA (Class). nullable (Class) matches null or an argument of the specified type, but ʻisA (Class) does not match null.

Target class annotations are now added to mock classes

For example, if the target class is

class Bar { ... }

The mock class is also given @Foo.

Bar mock = mock(Bar.class);

Strictness was introduced

Starting with Mockito 2.3, the Strictness class has been introduced.

--LENIENT (Mockito 1 default, loosest) --WARN (Mockito 2 default) --STRICT_STUBS (Mockito 3 will be the default, the toughest)

There are three modes, you can raise an exception about how to use the mock, and you can output a hint message.

Also for MockitoJUnitRunner

--MockitoJUnitRunner.Silent (equivalent to LENIENT) --MockitoJUnitRunner.Strict (equivalent to WARN, equivalent to MockitoJUnitRunner) --MockitoJUnitRunner.StrictStubs (equivalent to STRICT_STUBS)

Three classes have been added.

An example of use is shown below.

public class MyTest {

    public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);

    Foo foo;

    public void test() {

    class Foo {
        String bar() {
            return "";


The test () method above does not use the mock set in the when clause. Running this test throws an UnnecessaryStubbingException.

If you change STRICT_STUBS in the above code to WARN, no error will occur, but the following hint will be output to the console.

[MockitoHint] MyTest.test (see javadoc for MockitoHint):
[MockitoHint] 1. Unused -> at com.example.MyTest.test(MyTest.java:22)

If you change it to LENIENT, no error will occur and no message will be output.

The Mockito team [Highly recommended] to use STRICT_STUBS (https://javadoc.io/page/org.mockito/mockito-core/latest/org/mockito/quality/Strictness.html#STRICT_STUBS" )is.

Please note that the Strictness class is annotated with @Incubating.

Introduced Mockito Session

Starting with Mockito 2.7, Mockito Session is provided for environments where MockitoJUnitRunner / MockitoRule cannot be used. If you are using MockitoAnnotations # initMocks (Object) to generate the mock, you can replace it with MockitoSession.

The usage is shown below.

private MockitoSession session;

public void setUp() {
    session = Mockito.mockitoSession().initMocks(this).startMocking();

public void tearDown() {

If you do the above, the check according to the mode of Strictness will be performed at the timing of finishMocking ().

Use the strictness (Strictness) method to switch Strictness.

session = Mockito.mockitoSession()

The default for Strictness is STRICT_STUBS, which is a stricter mode than WARN in MockitoJUnitRunner / MockitoRule.

Please note that MockitoSession is also annotated with @Incubating.

that's all.

Recommended Posts

Changes in Mockito 2
Changes in mybatis-spring-boot-starter 2.0
Changes in mybatis-spring-boot-starter 2.1
Changes in mybatis-spring-boot-starter 1.3
Changes in Java 11
Changes in mybatis-spring-boot-starter 1.2
Org.mockito.exceptions.misusing.InvalidUseOfMatchersException in Mockito
Changes in JUnit5M4-> M5
Core Location changes in iOS 14
Major changes in Spring Boot 1.5
Mock static methods in Mockito 3.4
Java version notation that changes in Java 10
Try Mockito
Major changes in Spring Framework 5.0 core functionality