【问题标题】:How to setup Jacoco with Wildfly and Maven如何使用 Wildfly 和 Maven 设置 Jacoco
【发布时间】:2016-02-10 01:21:44
【问题描述】:

我尝试在我的 Eclipse IDE 中使用带有 Eclemma 插件的 Jacoco,但它不起作用。它在我使用 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>
....

有什么建议吗?

【问题讨论】:

    标签: java maven wildfly jboss-arquillian jacoco


    【解决方案1】:

    试试这个配置:

    <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。我不得不使用0.7.4.201502262128
    【解决方案2】:

    本指南将逐步向您展示如何在项目中设置 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 中使用。

    【讨论】:

    • 谢谢它可以工作,但使用旧版本的 Jacoco,0.7.4.201502262128。您提供的链接提到我们必须降级 Java 7 或更早项目的版本。这是我的情况。但与指定的不同,我可以使用0.7.x 版本。
    猜你喜欢
    • 2013-03-16
    • 2014-11-09
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2019-05-05
    • 2013-04-07
    • 2020-05-05
    • 2016-08-13
    相关资源
    最近更新 更多