Migration issues from JUnit 4 to JUnit 5
I'm migrating my code base from junit4 to junit5 I used mockito in my testcase Below is the different version I use for dependencies
<junit.jupiter.version>5.2.0</junit.jupiter.version> <junit.platform.version>1.2.0</junit.platform.version> <org.mockito.version>1.10.19</org.mockito.version> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>2.19.0</version> <scope>test</scope> </dependency>
I use the annotation @ runwith (mockitojunitrunner. Class) to run my mockito code Replace it with @ extendwith (mockitoextension. Class)
However, when I run the test case, I get the following error Any suggestions to solve this problem I wonder if there are any dependent version problems that cause this problem
java.lang.NoClassDefFoundError: org/mockito/quality/Strictness at java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) at java.lang.Class.getConstructor0(Class.java:3075) at java.lang.Class.getDeclaredConstructor(Class.java:2178) at..
Thank you, Sam
Solution
Junit5 mockitoextension uses org mockito. quality. Strictness, so in order to use mockitoextension, you need to include org mockito. quality. The mockito core version of strictness mockito-core:1.10. 19 this class is not included because it is in mockito 2 Added in X Therefore, in order to use mockitoextension, you need to use at least version 2 Mockito core of X
The mockito documentation does not specify, but I suspect that you will expect to use the same version of mockito for mockito core and mockito JUnit Jupiter
The following dependencies will allow you to successfully use junit5 mockitoextension:
<org.mockito.version>2.19.0</org.mockito.version> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>${org.mockito.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>${org.mockito.version}</version> <scope>test</scope> </dependency>