I created a simple spring-boot application to demonstrate the issue after I faced a similar issue in my product code and explained the problem using the Dummy project. I used testNg for writing unit tests of my spring-boot app and JMockit for the @Capturing feature of test classes. Then I wanted to use Jacoco to generate test coverage reports but after adding the Jacoco plugin reports folder and reports not generating (site/jacoco) I was able to see below console log
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes
because bootstrap classpath has been appended
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.263 s -- in
TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.10:report (report) @ testnginit ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
[INFO]
[INFO] --- maven-jar-plugin:3.3.0:jar (default-jar) @ testnginit ---
[INFO] Building jar: D:\project\testnginit\testnginit\target\testnginit-0.0.1-
SNAPSHOT.jar
then I commented below configuration of maven-surefire-plugin
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
</argLine>
</configuration>
after running the maven install command below error throws
[INFO] Running TestSuite
[ERROR] Tests run: 2, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 7.001 s <<<
FAILURE! -- in TestSuite
[ERROR] com.example.testnginit.AdditionTest.springTestContextBeforeTestMethod[public
void com.example.testnginit.AdditionTest.testAddition()](0) -- Time elapsed: 6.575 s <<<
FAILURE!
java.lang.NullPointerException: Cannot invoke
"java.lang.instrument.Instrumentation.retransformClasses(java.lang.Class[])" because
"mockit.internal.startup.Startup.instrumentation" is null
[ERROR] com.example.testnginit.AdditionTest.testAddition -- Time elapsed: 0.004 s <<<
FAILURE!
java.lang.NullPointerException: Cannot invoke
"java.lang.instrument.Instrumentation.retransformClasses(java.lang.Class[])" because
"mockit.internal.startup.Startup.instrumentation" is null
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] AdditionTest.springTestContextBeforeTestMethod » NullPointer Cannot invoke
"java.lang.instrument.Instrumentation.retransformClasses(java.lang.Class[])" because
"mockit.internal.startup.Startup.instrumentation" is null
[ERROR] AdditionTest.testAddition » NullPointer Cannot invoke
"java.lang.instrument.Instrumentation.retransformClasses(java.lang.Class[])" because
"mockit.internal.startup.Startup.instrumentation" is null
[INFO]
[ERROR] Tests run: 2, Failures: 2, Errors: 0, Skipped: 0
After I commented jmockit dependency and ran the maven install command the jacoco coverage report was successfully generated under site/jacoco directory.
I have tried with multiple jmockit versions to fix this and not worked can you help me sort this out, please? here is my source repository: https://github.com/manoj1995madushanka/jacoco-fix-with-surefire
My pom file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>testnginit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testnginit</name>
<description>testnginit</description>
<properties>
<java.version>17</java.version>
<jacoco-maven-plugin.version>0.8.10</jacoco-maven-plugin.version>
<testng.version>7.8.0</testng.version>
<jmockit.version>1.49</jmockit.version>
<test.includes>**/*Test.java</test.includes>
<test.excludes>**/*IT*.java</test.excludes>
<surefire-testng.version>3.2.2</surefire-testng.version>
<maven.compiler.version>3.11.0</maven.compiler.version>
<maven.surefire.version>3.1.2</maven.surefire.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire-testng.version}</version>
</dependency>
</dependencies>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
