How to "extend" JUnit 5
with mockito 3
?
In JUnit 5
to use mockito
priory to version 3.0
a test class needs to be annotated with a new JUnit 5
annotation @ExtendWith(MockitoExtension.class)
, e.g.:
@ExtendWith(MockitoExtension.class)
public class TestClass {
@Mock
DependencyA dependancyA;
public void myTest() {
...
}
}
and as such there is no need to use any more MockitoRule
with @Rule
annotation.
I have tried today beta release mockito-android 3.0.0-beta1
and it does not have MockitoExtension
class.
What should I use instead? I couldn't find any documentation for version 3.0, which is understandable as it is still in beta.
The MockitoExtension
is published in the mockito-junit-jupiter
artifact.
You can add a dependency on it as follows.
Maven
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.27.0</version>
<scope>test</scope>
</dependency>
Gradle
testCompile 'org.mockito:mockito-junit-jupiter:2.27.0'
Further Resources
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With