2017-09-22 99 views
1

我正在嘗試獲取集成測試的代碼覆蓋率報告。 Jacoco maven插件能夠爲單元測試提供代碼覆蓋率,但是爲集成測試提供0%的覆蓋率。集成測試正在部署在tomcat中的應用程序的終點。Jacoco Maven離線設備 - Tomcat

我的maven jacoco插件& surefire插件看起來像這樣。

<plugin> 
      <groupId>org.jacoco</groupId> 
      <artifactId>jacoco-maven-plugin</artifactId> 
      <version>0.7.9</version> 
      <executions> 
       <execution> 
        <id>prepare-agent</id> 
        <goals> 
         <goal>prepare-agent</goal> 
        </goals> 
       </execution> 
       <execution> 
        <id>prepare-agent-integration</id> 
        <goals> 
         <goal>prepare-agent-integration</goal> 
        </goals> 
       </execution> 
       <execution> 
        <id>report</id> 
        <phase>prepare-package</phase> 
        <goals> 
         <goal>report</goal> 
        </goals> 
       </execution> 
       <execution> 
        <id>post-unit-test</id> 
        <phase>integration-test</phase> 
        <goals> 
         <goal>report</goal> 
        </goals> 
        <configuration> 
         <dataFile>${project.build.directory}/target/jacoco-it.exec</dataFile> 
         <outputDirectory>${project.build.directory}/target/jacoco-ut</outputDirectory> 
        </configuration> 
       </execution> 
      </executions> 

      <configuration> 
       <systemPropertyVariables> 
        <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> 
       </systemPropertyVariables> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.20</version> 
      <configuration> 
       <!-- <skip>true</skip> --> 
       <!-- <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> 
        </systemPropertyVariables> --> 
      </configuration> 
      <!-- <configuration> <skip>true</skip> </configuration> --> 
      <executions> 
       <execution> 
        <id>unit-tests</id> 
        <phase>test</phase> 
        <goals> 
         <goal>test</goal> 
        </goals> 
        <configuration> 
         <!-- Never skip running the tests when the test phase is invoked --> 
         <!-- <skip>true</skip> --> 
         <argLine>@{argLine} 
          -javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse 
          2016 CI\\JaxRsApp\\target\\jacoco.exec</argLine> 
         <includes> 
          <include>**/*UnitTest.java</include> 
         </includes> 
         <excludes> 
          <exclude>**/*IntegrationTest.java</exclude> 
         </excludes> 
        </configuration> 
       </execution> 
       <execution> 
        <id>integration-tests</id> 
        <phase>integration-test</phase> 
        <goals> 
         <goal>test</goal> 
        </goals> 
        <configuration> 
         <!-- Never skip running the tests when the integration-test phase 
          is invoked --> 
         <!-- argLine>-javaagent:$WORKSPACE/target/lib/jacoco-agent-0.7.9.jar=includes=*,destfile=*/jacoco-coverage.exec,append=false</argLine --> 
         <skip>false</skip> 
         <argLine>@{argLine} 
          -javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse 
          2016 CI\\JaxRsApp\\target\\jacoco-it.exec 
         </argLine> 

         <includes> 
          <include>**/*IntegrationTest.java</include> 
         </includes> 
         <excludes> 
          <exclude>**/*UnitTest.java</exclude> 
         </excludes> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 

我執行單元測試,在測試階段中集成測試階段&集成測試。 我得到的最新錯誤是「由於缺少執行數據文件而跳過JaCoCo執行程序」。

回答

0

在Evgeny的幫助下,我做了這項工作。 將服務器更改爲glassfish & ide to intellij以便於調試。

使用以下JVM選項啓動glassfish服務器。

-DargLine=-javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:/dmahapat_JaxRsApp/target/coverage-reports/jacoco-it.exec 

更新POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 

<modelVersion>4.0.0</modelVersion> 

<groupId>org.mathworks</groupId> 
<artifactId>JaxRsApp</artifactId> 
<packaging>war</packaging> 
<version>0.0.1-SNAPSHOT</version> 
<name>JaxRsApp</name> 

<build> 
    <finalName>JaxRsApp</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.5.1</version> 
      <inherited>true</inherited> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>build-helper-maven-plugin</artifactId> 
      <version>3.0.0</version> 
      <executions> 
       <execution> 
        <id>add-test-source</id> 
        <goals> 
         <goal>add-test-source</goal> 
        </goals> 
        <configuration> 
         <sources> 
          <source>${project.basedir}\src\integration-test\java</source> 
         </sources> 
        </configuration> 
       </execution> 
       <execution> 
        <id>add-test-resource</id> 
        <goals> 
         <goal>add-test-resource</goal> 
        </goals> 
        <configuration> 
         <resources> 
          <resource> 
           <!-- Don't forget <directory> label --> 
           <directory>${project.basedir}\src\integration-test\resources</directory> 
          </resource> 
         </resources> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
     <groupId>org.jacoco</groupId> 
     <artifactId>jacoco-maven-plugin</artifactId> 
     <version>0.7.9</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> 

       <execution> 
        <id>pre-integration-test</id> 
        <phase>pre-integration-test</phase> 
        <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-it.exec</destFile> 
         <!-- 
          Sets the name of the property containing the settings 
          for JaCoCo runtime agent. 
         --> 
         <propertyName>failsafeArgLine</propertyName> 
        </configuration> 
       </execution> 
       <!-- 
        Ensures that the code coverage report for integration tests after 
        integration tests have been run. 
       --> 
       <execution> 
        <id>post-integration-test</id> 
        <phase>post-integration-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-it.exec</dataFile> 
         <!-- Sets the output directory for the code coverage report. --> 
         <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory> 
        </configuration> 
       </execution> 
      </executions> 

     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.20.1</version> 
      <configuration> 
       <argLine>${surefireArgLine}</argLine> 
       <excludes> 
        <exclude>**/*IntegrationTest*</exclude> 
       </excludes> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-failsafe-plugin</artifactId> 
      <version>2.20.1</version> 
      <configuration> 
       <argLine>${failsafeArgLine}</argLine> 
       <includes> 
        <include>**/*IntegrationTest.java</include> 
       </includes> 
       <excludes> 
        <exclude>**/*UnitTest.java</exclude> 
       </excludes> 
      </configuration> 
      <executions> 
       <execution> 
        <id>integration-tests</id> 
        <phase>integration-test</phase> 
        <goals> 
         <goal>integration-test</goal> 
         <goal>verify</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.glassfish.jersey</groupId> 
      <artifactId>jersey-bom</artifactId> 
      <version>${jersey.version}</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

<dependencies> 
    <dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-servlet-core</artifactId> 
     <!-- use the following artifactId if you don't need servlet 2.x compatibility --> 
     <!-- artifactId>jersey-container-servlet</artifactId --> 
    </dependency> 

    <dependency> 
     <groupId>org.glassfish.jersey.media</groupId> 
     <artifactId>jersey-media-moxy</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.12</version> 
     <scope>test</scope> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> 
    <dependency> 
     <groupId>org.apache.httpcomponents</groupId> 
     <artifactId>httpclient</artifactId> 
     <version>4.3.5</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime --> 
    <dependency> 
     <groupId>org.apache.httpcomponents</groupId> 
     <artifactId>httpmime</artifactId> 
     <version>4.5.3</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>2.8.7</version> 
    </dependency> 
</dependencies> 
<properties> 
    <jersey.version>2.25.1</jersey.version> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 

</properties> 

0

引用documentation of prepare-agent-integration

同製備劑,但提供了合適的默認值的集成的測試:

  • 結合到預集成測試階段
  • 不同destFile

報價documentation of prepare-agent

準備指向JaCoCo運行劑,可以是如被測一個VM參數傳遞給應用程序傳遞一個屬性。根據默認情況下,項目包裝類型,有下列名稱的屬性設置:

  • tycho.testArgLine包裝類型Eclipse的測試插件和
  • argLine否則。

在大多數情況下argLine自動由maven-surefire-plugin拾取啓動JVM執行單元測試 - http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine

argLine:任意JVM選項在命令行上設置。

這解釋了爲什麼你會得到單元測試的覆蓋率。

要獲得集成測試的覆蓋範圍,您必須確保將此屬性傳遞給測試中的應用程序的JVM,即執行Tomcat的JVM,完全取決於您啓動它的方式。

+0

我是否需要提供以下參數-javaagent啓動Tomcat:C:\\ \\ IAT \\ mavenrepository組織\\ \\ jacoco組織。 jacoco.agent \\ 0.7.10-SNAPSHOT \\ org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar = destfile = C:\\ Users \\ dmahapat \\ Workspaces \\ MyEclipse2016 CI \\ JaxRsApp \\ target \\ jacoco-it.exec –

+0

如果您使用Maven啓動Tomcat,那麼'jacoco-maven-plugin'爲您準備好這個參數,所以您只需要像平常一樣將它傳遞給Tomcat Maven屬性 - '$ {argLine}'。 – Godin

+0

@debajyotimahapatro請不要忘記upvote有幫助,並接受+ upvote正確的答案,成爲本網站的好用戶。 – Godin