Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migration issue from Junit 4 to Junit 5

I am migrating my codebase from junit4 to junit5.I have used mockito in my testcase.Below are the different version that i am using for the dependency.

<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 have used the annotation @RunWith(MockitoJUnitRunner.class) to run my mockito code.Replaced the same with @ExtendWith(MockitoExtension.class)

But when i run the test case i get the below error. Any suggestion to solve this issue. I suspect is there any dependency version issue which is causing 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..

Thanks -Sam

like image 220
Sam Avatar asked Sep 27 '18 11:09

Sam


People also ask

How to migrate from JUnit 4 to JUnit 5?

JUnit provides a gradual migration path with the help of JUnit Vintage test engine. We can use the JUnit Vintage test engine to run JUnit 4 tests with JUnit 5. All classes specific to JUnit 4 are located in org.junit package. All classes specific to JUnit 5 are located in the org.junit.jupiter package.

How do I migrate code from JUnit to IntelliJ IDEA?

IntelliJ IDEA’s inspections IntelliJ IDEA’s inspections can be very helpful for migrating code. In particular there are a number of inspections for JUnit tests. To help with the migration, turn on ‘JUnit 4 test can be JUnit 5’ inspection. You can only turn this on if you’re using at least Java 8 and have JUnit 5 dependencies set up.

Does JUnit-vintage-engine support JUnit 4 test cases?

JUnit 5 has introduced junit-vintage-engine to support JUnit 4 test cases. You have to exclude this dependency of vintage engine so that only available engine is of JUnit 5. Open CalculatorTest from the copied project and paste the snippet provided below. There are certain things worth noting here.

What is @extendwith In JUnit 5?

The @RunWith was used to integrate the test context with other frameworks or to change the overall execution flow in the test cases in JUnit 4. With JUnit 5, we can now use the @ExtendWith annotation to provide similar functionality. As an example, to use the Spring features in JUnit 4:


1 Answers

The JUnit5 MockitoExtension uses org.mockito.quality.Strictness so in order to use MockitoExtension you'll need to use a version of mockito-core which contains org.mockito.quality.Strictness. mockito-core:1.10.19 does not contain that class because that class was added in Mockito 2.x. So, in order to use MockitoExtension you'll need to use at least version 2.x of mockito-core.

The Mockito docs don't make this explicit but I suspect the expectation is that you'll use the same Mockito version for mockito-core and for mockito-junit-jupiter.

The following dependencies will allow you to use the JUnit5 MockitoExtension successfully:

<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>
like image 177
glytching Avatar answered Jan 02 '23 03:01

glytching