如何使用 Wildfly 和 Maven 设置 Jacoco

How to setup Jacoco with Wildfly and Maven

我尝试在我的 Eclipse IDE 中将 Jacoco 与 Eclemma 插件一起使用,但它不起作用。它在我使用 JBoss 7 但不再使用 Wildfly 9 时有效。我可以 运行 我的 JUnit 测试没有错误,但代码覆盖率始终为 0%。我正在使用 arquillian。这就是我的 pom.xml :

...
<properties>
    <version.jacoco>0.7.5.201505241946</version.jacoco>
</properties>
...
<dependencies>
    ...
       <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.wildfly</groupId>
           <artifactId>wildfly-jms-client-bom</artifactId>
           <version>9.0.1.Final</version>
           <type>pom</type>
           <scope>provided</scope>
       </dependency>

       <dependency>
           <groupId>org.jboss.arquillian.extension</groupId>
           <artifactId>arquillian-jacoco</artifactId>
           <version>1.0.0.Alpha8</version>
           <scope>test</scope>
       </dependency>

       <dependency>
           <groupId>org.jacoco</groupId>
           <artifactId>org.jacoco.core</artifactId>
           <version>${version.jacoco}</version>
           <scope>test</scope>
       </dependency>
    ...
</dependencies>

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${version.jacoco}</version>
                <executions>
                    <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        ...
    </plugins>
</build>

<profiles>
    ...
    <profile>
        <id>arq-wildfly-managed</id>
        <dependencies>
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-arquillian-container-managed</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </profile>

    <profile>
        <id>arq-wildfly-remote</id>
        <dependencies>
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-arquillian-container-remote</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </profile>

</profiles>
....

有什么建议吗?

试试这个配置:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Surefire plugin is executed.
        -->
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <!--
            Ensures that the code coverage report for unit tests is created after
            unit tests have been run.
        -->
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-failsafe-plugin</artifactId>
     <version>2.15</version>
     <configuration>
         <!-- Sets the VM argument line used when integration tests are run. -->
         <argLine>${failsafeArgLine}</argLine>
     </configuration>
 </plugin>

本指南逐步向您展示如何在您的项目中设置 Jacoco:http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/

第 1 步:使用 jacoco-maven-plugin:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Surefire plugin is executed.
        -->
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <!--
            Ensures that the code coverage report for unit tests is created after
            unit tests have been run.
        -->
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

第 2 步:使用 maven-surefire-plugin

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
    <!-- Sets the VM argument line used when unit tests are run. -->
    <argLine>${surefireArgLine}</argLine>
    <!-- Skips unit tests if the value of skip.unit.tests property is true -->
    <skipTests>${skip.unit.tests}</skipTests>
    <!-- Excludes integration tests when unit tests are run. -->
    <excludes>
        <exclude>**/IT*.java</exclude>
    </excludes>
</configuration>

注意 surefireArgline 属性,它在 jacoco-maven-plugin 中定义,并在 maven-surefire-plugin 中使用。