Jacoco + Surefire Maven插件始终显示0%的覆盖率

时间:2020-06-16 13:02:42

标签: maven jacoco maven-surefire-plugin jboss-arquillian jacoco-maven-plugin

我有一个多模块Maven项目,我想在该项目上生成包含单位覆盖率的jacoco报告。目前,我正在使用arqullian surefire和jacoco maven插件,它们都仅在一个模块中定义。测试在JBoss EAP 7.2上运行

尽管生成了jacoco报告,但我始终将所有测试类的覆盖率都设置为0%,这些类被部署为JBoss服务器上的存档 org.jboss.shrinkwrap.api.spec.WebArchive 即使单元测试涵盖了几种方法。
重要:未经部署而执行的其他单元测试确实在报告中显示了一些内容

enter image description here

下面是我的Maven配置

<plugins>
   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.22.2</version>
      <configuration>
         <skip>${skipTests}</skip>
         <argLine>${surefireArgLine}</argLine>
      </configuration>
   </plugin>
   <plugin>
      <artifactId>maven-surefire-report-plugin</artifactId>
      <version>2.22.2</version>
   </plugin>
   <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.5</version>
      <executions>
         <execution>
            <id>pre-unit-test</id>
            <goals>
               <goal>prepare-agent</goal>
            </goals>
            <configuration>
               <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
               <propertyName>surefireArgLine</propertyName>
            </configuration>
         </execution>
         <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
               <goal>report</goal>
            </goals>
            <configuration>
               <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
               <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
         </execution>
      </executions>
   </plugin>
</plugins>

在我使用的依赖项下面:

<dependency>
    <groupId>org.dbunit</groupId>
    <artifactId>dbunit</artifactId>
    <version>2.7.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jboss.shrinkwrap.resolver</groupId>
    <artifactId>shrinkwrap-resolver-api-maven</artifactId>
    <version>3.1.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jboss.shrinkwrap.resolver</groupId>
    <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
    <version>3.1.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jboss.shrinkwrap.resolver</groupId>
    <artifactId>shrinkwrap-resolver-spi</artifactId>
    <version>3.1.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-core</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-container</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jboss.arquillian.protocol</groupId>
    <artifactId>arquillian-protocol-servlet</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>org.jacoco.core</artifactId>
    <version>0.8.5</version>
    <scope>test</scope>
</dependency>

档案XML

<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/schema/arquillian         http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
   <!-- Force the use of the Servlet 3.0 protocol with all containers, as it is the most mature -->
   <defaultProtocol type="Servlet 3.0" />
   <!-- Example configuration for a remote JBoss EAP 7 instance -->
   <container qualifier="jboss" default="true">
      <!-- If you want to use the JBOSS_HOME environment variable, just delete the jbossHome property -->
      <configuration>
         <!--   <property name="javaVmArguments">${jacoco.agent}</property>  -->
      </configuration>
   </container>
</arquillian>

在我的一种配置(不是以上配置)上,我记得看到一个警告,提示未使用$ {jacoco.agent}。所以我确实再次评论了。

运行mvn全新安装目标时,在控制台日志中,我看到以下变量设置为 surefireArgLine

[INFO] --- jacoco-maven-plugin:0.8.5:prepare-agent (pre-unit-test) @ alis-ejb ---
[INFO] surefireArgLine set to -javaagent:C:\\Users\\user\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.8.5\\org.jacoco.agent-0.8.5-runtime.jar=destfile=c:\\Projects\\Projectname\\ejb-module\\target\\coverage-reports\\jacoco-ut.exec

我所有的测试都正常执行,并且我能够生成带有surefire-report的surefire report命令,该命令列出了所有单元测试的执行情况。我正在考虑是否错过了jacoco用来计算覆盖率的mvn目标,但看来我仍然缺少某些东西

更新1 :我开始认为此问题是由于Arquilian的使用而引起的,我应该改用集成前测试阶段。但还不确定。

更新2 :我刚刚注意到,在我的一项单元测试中,我看到了一些覆盖范围。此单元测试与其余单元测试之间的区别是,一个包含测试的类(如图所示)是简单的单元测试,而对于其余的测试类,我没有从Jacoco获得覆盖报告。 > org.jboss.shrinkwrap.api.spec.WebArchive 部署已创建。一些更多的细节。我90%的测试都属于这种结构。将创建Webarchive并将其部署在JBoss EAP 7上。

0 个答案:

没有答案