Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mockito 3.0 with JUnit 5?

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.

like image 245
blekione Avatar asked Jan 02 '23 00:01

blekione


1 Answers

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

  • https://github.com/mockito/mockito/issues/1517#issuecomment-429621891
  • https://search.maven.org/artifact/org.mockito/mockito-junit-jupiter/2.27.0/jar
like image 145
Sam Brannen Avatar answered Jan 08 '23 02:01

Sam Brannen