2015-11-09 43 views
1

我嘗試在Eclmagn插件的Eclipse IDE中使用Jacoco,但它不起作用。它在我使用JBoss 7時起作用,但不再與Wildfly 9一起使用。我可以在沒有錯誤的情況下運行我的JUnit測試,但代碼覆蓋率始終爲0%。我正在使用arquillian。這是我在我的pom.xml文件中的內容:如何使用Wildfly和Maven設置Jacoco

... 
<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> 
.... 

任何建議?

回答

1

這個指南將向你一步一步設置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> 
    <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中使用。

+0

謝謝你的作品,但與舊版本的Jacoco,'0.7.4.201502262128'。您提供的鏈接提到我們必須降級Java 7或更早版本項目的版本。這是我的情況。但不同的是,我可以使用0.7.x版本。 – cheb1k4

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> 
+0

和以前一樣的結果。 – cheb1k4

+0

實際上,您的解決方案的工作原理是使用老版本的Jacoco。我不得不使用'0.7.4.201502262128'。 – cheb1k4

相關問題