每个测试设置的 jacoco 覆盖率

jacoco coverage per test setup

我正在使用 jacoco 来收集代码指标并将它们导入 sonarQube,但我缺少有关每次测试覆盖率的详细信息。所以在搜索之后,我找到了 this tutorial 但没能在我的项目中使用它。

这是我的 pom.xml

<properties>
    <sonarVersion>2.4</sonarVersion>
    <spring-framework.version>4.0.0.RELEASE</spring-framework.version>
    <spring-framework.security.version>3.2.5.RELEASE</spring-framework.security.version>
    <jackson.version>1.9.13</jackson.version>
    <jacoco.version>0.7.7.201606060606</jacoco.version>
    <aspectj.version>1.7.4</aspectj.version>
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.reportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPath>
    <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco-it.exec</sonar.jacoco.itReportPath>
    <sonar.jacoco.jar>${project.basedir}/libs/jacocoagent.jar</sonar.jacoco.jar>
    <sonar.jacoco.excludes>**/jaxb/**:**/generated/**:**/validators/**:**/dao/**:**/MBeanUtils/**</sonar.jacoco.excludes>
    <sonar.language>java</sonar.language>
    <jacoco-listeners.version>2.3</jacoco-listeners.version>
    <jacoco.utAgentConfig>-XX:-UseSplitVerifier -javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.reportPath},excludes=${sonar.jacoco.excludes}</jacoco.utAgentConfig>
    <jacoco.itAgentConfig>-javaagent:${sonar.jacoco.jar}=destfile=${sonar.jacoco.itReportPath},excludes=${sonar.jacoco.excludes}</jacoco.itAgentConfig>
</properties>

<dependencies>
    <dependency> 
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
       <groupId>javax.validation</groupId>
       <artifactId>validation-api</artifactId>
       <version>1.0.0.GA</version>
    </dependency>
    <dependency>
        <groupId>opendmk.java.net</groupId>
        <artifactId>jdmkrt</artifactId>
        <version>1.0-b02</version>
    </dependency>
    <dependency>
      <groupId>com.betfair.net.java.opendmk</groupId>
      <artifactId>core</artifactId>
      <version>1.0-b02</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>       
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.2</version>
        <exclusions>
            <exclusion>
              <groupId>c3p0</groupId>
              <artifactId>c3p0</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.1</version>
    </dependency>
    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-multipart-provider</artifactId>
        <version>2.3.7.Final</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.ibatis</groupId>
        <artifactId>ibatis-sqlmap</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.5.Final</version>
        <scope>provided</scope>
    </dependency>
    <!-- SPRING -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring-framework.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-crypto</artifactId>
        <version>${spring-framework.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring-framework.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring-framework.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring-framework.security.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- LOG4J -->  
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
   </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
   </dependency>
   <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>${aspectj.version}</version>
   </dependency>
    <!-- JSON --> 
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <!-- Need for LicenseUtils -->  
    <dependency>
        <groupId>org.jdom</groupId>
        <artifactId>jdom</artifactId>
        <version>1.1</version>
    </dependency>
    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- DBUnit -->
    <dependency>
        <groupId>org.dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>2.5.1</version>
    </dependency>   
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
    </dependency>       
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>httpunit</groupId>
        <artifactId>httpunit</artifactId>
        <version>1.7</version>
    </dependency>           
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>   
    <dependency>
       <groupId>org.codehaus.sonar-plugins.java</groupId>
       <artifactId>sonar-jacoco-listeners</artifactId>
       <version>${jacoco-listeners.version}</version>
       <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-utils</artifactId>
                    <version>1.1</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>   
            <configuration>
               <skip>true</skip>
               <trimStackTrace>false</trimStackTrace>
            </configuration>                
            <executions>
                <execution>
                    <id>unit-tests</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <argLine>${jacoco.utAgentConfig}</argLine>
                        <testFailureIgnore>true</testFailureIgnore>                     
                        <skip>false</skip>
                        <includes>
                            <include>**/*Test.java</include>
                        </includes> 
                        <properties>
                            <property>
                                <name>listener</name>
                                <value>org.sonar.java.jacoco.JUnitListener</value>
                            </property>
                        </properties>                                       
                    </configuration>                        
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如果我删除这个块它运行正常:

<properties>
    <property>
        <name>listener</name>
        <value>org.sonar.java.jacoco.JUnitListener</value>
    </property>
</properties>

但此块失败并给出以下错误:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (unit-tests) on project: Execution unit-tests of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process
org.apache.maven.surefire.util.SurefireReflectionException: org.sonar.java.jacoco.JacocoController$JacocoControllerError: Unable to access JaCoCo Agent - make sure that you use JaCoCo and version not lower than 0.6.2.
    at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:135)
    at org.apache.maven.surefire.booter.ForkedBooter.createProviderInCurrentClassloader(ForkedBooter.java:321)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
Caused by: org.sonar.java.jacoco.JacocoController$JacocoControllerError: Unable to access JaCoCo Agent - make sure that you use JaCoCo and version not lower than 0.6.2.
    at org.sonar.java.jacoco.JacocoController.<init>(JacocoController.java:48)
    at org.sonar.java.jacoco.JacocoController.getInstance(JacocoController.java:39)
    at org.sonar.java.jacoco.JUnitListener.<init>(JUnitListener.java:33)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:383)
    at org.apache.maven.surefire.util.ReflectionUtils.instantiate(ReflectionUtils.java:112)
    at org.apache.maven.surefire.common.junit4.JUnit4RunListenerFactory.createCustomListeners(JUnit4RunListenerFactory.java:45)
    at org.apache.maven.surefire.junit4.JUnit4Provider.<init>(JUnit4Provider.java:110)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:131)
    ... 4 more
Caused by: java.lang.NoClassDefFoundError: org/jacoco/agent/rt/RT
    at org.sonar.java.jacoco.JacocoController.<init>(JacocoController.java:46)
    ... 19 more
Caused by: java.lang.ClassNotFoundException: org.jacoco.agent.rt.RT
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 20 more

    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:224)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution unit-tests of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process
org.apache.maven.surefire.util.SurefireReflectionException: org.sonar.java.jacoco.JacocoController$JacocoControllerError: Unable to access JaCoCo Agent - make sure that you use JaCoCo and version not lower than 0.6.2.
    at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:135)
    at org.apache.maven.surefire.booter.ForkedBooter.createProviderInCurrentClassloader(ForkedBooter.java:321)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
Caused by: org.sonar.java.jacoco.JacocoController$JacocoControllerError: Unable to access JaCoCo Agent - make sure that you use JaCoCo and version not lower than 0.6.2.
    at org.sonar.java.jacoco.JacocoController.<init>(JacocoController.java:48)
    at org.sonar.java.jacoco.JacocoController.getInstance(JacocoController.java:39)
    at org.sonar.java.jacoco.JUnitListener.<init>(JUnitListener.java:33)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:383)
    at org.apache.maven.surefire.util.ReflectionUtils.instantiate(ReflectionUtils.java:112)
    at org.apache.maven.surefire.common.junit4.JUnit4RunListenerFactory.createCustomListeners(JUnit4RunListenerFactory.java:45)
    at org.apache.maven.surefire.junit4.JUnit4Provider.<init>(JUnit4Provider.java:110)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:131)
    ... 4 more
Caused by: java.lang.NoClassDefFoundError: org/jacoco/agent/rt/RT
    at org.sonar.java.jacoco.JacocoController.<init>(JacocoController.java:46)
    ... 19 more
Caused by: java.lang.ClassNotFoundException: org.jacoco.agent.rt.RT
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 20 more

    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:143)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 19 more
Caused by: java.lang.RuntimeException: There was an error in the forked process
org.apache.maven.surefire.util.SurefireReflectionException: org.sonar.java.jacoco.JacocoController$JacocoControllerError: Unable to access JaCoCo Agent - make sure that you use JaCoCo and version not lower than 0.6.2.
    at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:135)
    at org.apache.maven.surefire.booter.ForkedBooter.createProviderInCurrentClassloader(ForkedBooter.java:321)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
Caused by: org.sonar.java.jacoco.JacocoController$JacocoControllerError: Unable to access JaCoCo Agent - make sure that you use JaCoCo and version not lower than 0.6.2.
    at org.sonar.java.jacoco.JacocoController.<init>(JacocoController.java:48)
    at org.sonar.java.jacoco.JacocoController.getInstance(JacocoController.java:39)
    at org.sonar.java.jacoco.JUnitListener.<init>(JUnitListener.java:33)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:383)
    at org.apache.maven.surefire.util.ReflectionUtils.instantiate(ReflectionUtils.java:112)
    at org.apache.maven.surefire.common.junit4.JUnit4RunListenerFactory.createCustomListeners(JUnit4RunListenerFactory.java:45)
    at org.apache.maven.surefire.junit4.JUnit4Provider.<init>(JUnit4Provider.java:110)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:131)
    ... 4 more
Caused by: java.lang.NoClassDefFoundError: org/jacoco/agent/rt/RT
    at org.sonar.java.jacoco.JacocoController.<init>(JacocoController.java:46)
    ... 19 more
Caused by: java.lang.ClassNotFoundException: org.jacoco.agent.rt.RT
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 20 more

    at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:584)
    at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:460)
    at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:229)
    at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:201)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1026)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:862)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:755)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
    ... 20 more

感谢您的帮助。

您确定这是您的 surefire 配置吗?

如果我的 POM 在 surefire 配置中包含 <reuseForks>false</reuseForks>org.sonar.java.jacoco.JUnitListener,我会得到相同的结果:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${surefire.version}</version>
            <configuration>
                <reuseForks>false</reuseForks>
                <!-- Headless - prevent the annoying ForkedBooter process from stealing window focus on Mac OS -->
                <argLine>${surefireArgLine} -Djava.awt.headless=true</argLine>
                <skipTests>${skip.unit.tests}</skipTests>
                <includes>
                    <include>au/gov/ga/geodesy/**/*.java</include>
                </includes>
                <properties>
                    <property>
                        <name>listener</name>                                
                        <value>org.sonar.java.jacoco.JUnitListener</value>
                    </property>
                </properties>
            </configuration>
        </plugin>

您可以放弃 <reuseForks>false</reuseForks> 或切换到似乎不会遇到此问题的 TestNG:

         <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.4</version>
            <scope>test</scope>
        </dependency>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <forkCount>1</forkCount>
                <!-- Headless - prevent the annoying ForkedBooter process from stealing window focus on Mac OS -->
                <argLine>${surefireArgLine} -Djava.awt.headless=true</argLine>
                <skipTests>${skip.unit.tests}</skipTests>
                <includes>
                    <include>au/gov/ga/geodesy/**/*.java</include>
                </includes>
                <properties>
                    <property>
                        <name>listener</name>
                        <value>org.sonar.java.jacoco.TestNGListener</value>
                    </property>
                </properties>
            </configuration>
        </plugin>

当我戴着开发人员的帽子时,TestNG 和 Junit 对我来说真正的区别在于 assertEquals 的参数顺序。这显然很烦人。但是现在我们(我的团队!)采用了使用 Hamcrest 的做法,这样这就不再是一个问题了:

因此:

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;

...

@Test public void theTest() {
    assertNotNull(manifestService.getExternalManifests());
    assertEquals(1, manifestService.getExternalManifests().size());

变成:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.IsNot.not;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

....

@Test public void theTest() {
    assertThat(manifestService.getExternalManifests(), not(nullValue()));
    assertThat(manifestService.getExternalManifests().size(), is(1));

添加 jacoco-maven-plugin 因为那是使用监听器的东西,它似乎在你的 pom.xml 中丢失了。像这样:

           <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.7.201606060606</version>
                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <!--<phase>test</phase>-->
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to the file to write the execution data to. -->
                            <destFile>${sonar.jacoco.reportPath}</destFile>
                            <!-- Connection with SureFire plugin -->
                            <propertyName>sonarUnitTestArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to where the execution data is located. -->
                            <dataFile>${sonar.jacoco.reportPath}</dataFile>
                            <!-- Sets the output directory for the code coverage report. -->
                            <outputDirectory>${jacoco.ut.outputdir}</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>pre-integration-test</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to the file to write the execution data to. -->
                            <destFile>${sonar.jacoco.itReportPath}</destFile>
                            <!-- Connection with Failsafe plugin -->
                            <propertyName>sonarIntegrationTestArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>report-integration</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to where the execution data is located. -->
                            <dataFile>${sonar.jacoco.itReportPath}</dataFile>
                            <!-- Sets the output directory for the code coverage report. -->
                            <outputDirectory>${jacoco.it.outputdir}</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <dataFile>${sonar.jacoco.itutCombinedReportPath}</dataFile>
                            <haltOnFailure>true</haltOnFailure>
                            <rules>
                                <!--  implementation is needed only for Maven 2  -->
                                <rule implementation="org.jacoco.maven.RuleConfiguration">
                                    <element>BUNDLE</element>
                                    <limits>
                                        <!--  Bump this up.  Currently a very low 0.01 -->
                                        <limit implementation="org.jacoco.report.check.Limit">
                                            <counter>COMPLEXITY</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0.01</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

我的属性是:

<properties>
   ...
    <!-- Sonar static analysis / Jacoco code coverage -->
    <sonar.host.url>https://sonar.domain.com/</sonar.host.url>
    <sonar.projectKey>${project.artifactId}</sonar.projectKey>
    <sonar.projectName>${project.artifactId}</sonar.projectName>
    <sonar.projectVersion>${project.version}</sonar.projectVersion>
    <sonar.language>java</sonar.language>
    <sonar.sources>src/main/java</sonar.sources>
    <sonar.test>target/test-classes</sonar.test>
    <sonar.scm.provider>git</sonar.scm.provider>
    <sonar.login>${SONAR_LOGIN}</sonar.login>
    <jacoco.basedir>${project.build.directory}/code-coverage</jacoco.basedir>
    <!-- Define location of Java CodeCoverage data from Unit Tests (run using SureFire) -->
    <sonar.jacoco.reportPath>${jacoco.basedir}/jacoco-ut.exec</sonar.jacoco.reportPath>
    <jacoco.ut.outputdir>${jacoco.basedir}/jacoco-ut-out</jacoco.ut.outputdir>
    <!-- Define location of Java CodeCoverage data from Integration Tests (run using Failsafe) -->
    <sonar.jacoco.itReportPath>${jacoco.basedir}/jacoco-it.exec</sonar.jacoco.itReportPath>
    <jacoco.it.outputdir>${jacoco.basedir}/jacoco-it-out</jacoco.it.outputdir>
    <!-- Combined report file -->
    <sonar.jacoco.itutCombinedReportPath>${jacoco.basedir}/jacoco-itut-combined.exec</sonar.jacoco.itutCombinedReportPath>
</properties>

作为记录,这是我的 AntRun 插件,它创建了 itUtCombined 报告文件。警告 - 我还没有完成测试以确认它是否正常工作(感谢反馈!):

         <plugin>
                <!--The JaCoCo report files need to be combined into one for the code coverage check-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>post-integration-test</phase>
                        <configuration>
                            <target>
                                <!--<copy file="${sonar.jacoco.reportPath}" tofile="${sonar.jacoco.itutCombinedReportPath}"/>-->
                                <concat destfile="${sonar.jacoco.itutCombinedReportPath}" append="true" binary="yes">
                                    <filelist files="${sonar.jacoco.reportPath},${sonar.jacoco.itReportPath}"/>
                                </concat>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

(这来自我在第一个答案中的评论,我保留了这些评论,因为它在某些情况下是相关的答案 -