5

I am converting the unit tests written in JUnit 4 to JUnit 5. We use JMockit when a method being tested makes a call to static method. In JUnit 4,

@RunWith(JMockit.class)
public class MyTest{

}

In JUnit 5 if I add JMocket dependency in pom.xml, JUnit 5 tests which use JMockit and the ones which do not use JMockit fails with errorenter image description here

ERROR
error java.lang.NullPointerException
  at mockit.integration.junit4.internal.RunNotifierDecorator.fireTestStarted.

If I remove JMockit dependency from pom.xml, the JUnit 5 tests are successful for the ones that do not use JMockit. But others fail with error,

 mockit cannot be found.

<jmockit-version>1.38</jmockit-version>     
<junit-jupiter-version>5.2.0</junit-jupiter-version>
<maven-surefire-plugin>2.21.0</maven-surefire-plugin>
<junit-platform-runner-version>1.2.0</junit-platform-runner-version>
<junit-platform-surefire-version>1.2.0</junit-platform-surefire-version>
<mockito-junit-jupiter-version>2.18.3</mockito-junit-jupiter-version>

 <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${maven-surefire-plugin}</version>
  <configuration>
   <argLine> javaagent:"${settings.localRepository}"/org/jmockit/jmockit/${jmockit-version}/jmockit-${jmockit-version}.jar
                            -XX:-UseSplitVerifier -Xmx512m -XX:MaxPermSize=256m ${argLine}
   </argLine>
 </configuration>
</plugin>

<!-- Testing -->
    <dependency>
        <groupId>org.jmockit</groupId>
        <artifactId>jmockit</artifactId>
        <version>${jmockit-version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit-jupiter-version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit-jupiter-version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit-platform-runner-version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>${mockito-junit-jupiter-version}</version>
        <scope>test</scope>
    </dependency>
Tony Weddle
  • 2,081
  • 1
  • 11
  • 15
user679526
  • 845
  • 4
  • 16
  • 39

1 Answers1

3

If the actual question is "how to use JMockit from JUnit 5", then the answer is to simply use it normally - nothing special required. See the Tutorial.

Rogério
  • 16,171
  • 2
  • 50
  • 63