这就是我们解决它的方法:
摘要:
- 服务和服务测试项目是两个独立的模块。
- 服务具有单元测试,Sonar 报告覆盖率。
- Service-itests 使用 Cargo 加载服务 WAR 并运行集成测试。在此执行中收集的代码覆盖率在上一级报告。通过这种方式,ITests 的覆盖率被收集并跨模块合并,并在父项目 pom 级别报告。我们也喜欢这种方法,因为它允许我们拥有不同的 ITests 项目(例如,一个使用 Cargo 加载应用程序,另一个使用 JBehave),并且它们可以独立发展。
在父 pom 中:
<modules>
<module>service</module>
<module></module>
<module>service-itest</module>
</modules>
<!-- Sonar -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<!-- The destination file for the code coverage report has to be set to
the same value in the parent pom and in each module pom. Then JaCoCo will
add up information in the same report, so that, it will give the cross-module
code coverage. -->
<sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-itests.exec</sonar.jacoco.itReportPath>
...
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
</plugin>
</plugins>
</pluginManagement>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<includes>
<include>our.project.packages.*</include>
</includes>
</configuration>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
在 -itest 项目的 pom.xml 文件中:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<!-- The destination file for the code coverage report has to be set
to the same value in the parent pom and in each module pom. Then JaCoCo will
add up information in the same report, so that, it will give the cross-module
code coverage. -->
<destFile>${project.basedir}/../target/jacoco-itests.exec</destFile>
<includes>
<include>our.packages.*</include>
</includes>
</configuration>
<executions>
<execution>
<id>post-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<skip>${cargo.skip}</skip>
<container>
<containerId>jetty7x</containerId>
<artifactInstaller>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-distribution</artifactId>
<version>7.6.12.v20130726</version>
<type>zip</type>
</artifactInstaller>
</container>
<configuration>
<type>standalone</type>
<properties>
<cargo.servlet.port>${jetty.port}</cargo.servlet.port>
<cargo.jvmargs>${argLine}</cargo.jvmargs>
</properties>
<deployables>
<deployable>
<artifactId>pam_filetask_manager_service</artifactId>
<groupId>${project.groupId}</groupId>
<type>war</type>
<pingURL>http://server:22000/ping</pingURL>
<properties>
</properties>
</deployable>
</deployables>
</configuration>
</configuration>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>install</goal>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>