About the changes in Mockito 2. The content is for Mockito 1 users.
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.
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).
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.
text:app/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
mock-maker-inline
Alternatively, specify the mockito-inline artifact instead of mockito-core.
build.gradle
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.
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.
build.gradle
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.
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 ()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.
AnyXxx () matchers such as ʻanyString ()and ʻanyList ()
no longer match null.
Mockito 2 uses ʻany () `.
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.
For example, if the target class is
@Foo
class Bar { ... }
The mock class is also given @Foo
.
Bar mock = mock(Bar.class);
assertTrue(mock.getClass().isAnnotationPresent(Foo.class));
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 {
@Rule
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Mock
Foo foo;
@Test
public void test() {
when(foo.bar()).thenReturn("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
.
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;
@Before
public void setUp() {
session = Mockito.mockitoSession().initMocks(this).startMocking();
}
@After
public void tearDown() {
session.finishMocking();
}
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()
.initMocks(this)
.strictness(Strictness.WARN)
.startMocking();
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