Goal report 仅对当前模块中的类进行分析,并创建仅包含它们的报告,即使记录了其他类的数据。还有goal report-aggregate
reactor 内多个项目的覆盖率报告
(见下文示例)
同样回到 PowerMockito - 正如https://stackoverflow.com/a/42305077/244993 中所说,它增加了额外的复杂性,但是:
a/src/main/java/example/A.java:
package example;
class A {
// to be tested in module "a"
void a() {
System.out.println("A_a" + fun());
}
// to be tested in module "b"
void b() {
System.out.println("A_b" + fun());
}
// to be mocked by PowerMockito
static String fun() {
return "";
}
}
a/src/test/java/example/ATest.java:
package example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@PrepareForTest({A.class})
@RunWith(PowerMockRunner.class)
public class ATest {
@Test
public void test() {
PowerMockito.mockStatic(A.class);
Mockito.when(A.fun()).thenReturn("fun");
new A().a();
}
}
b/src/main/java/example/B.java:
package example;
class B {
// to be tested in module "b"
void b() {
System.out.println("B_b" + fun());
}
// to be mocked by PowerMockito
static String fun() {
return "";
}
}
b/src/test/java/example/BTest.java:
package example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@PrepareForTest({A.class, B.class})
@RunWith(PowerMockRunner.class)
public class BTest {
@Test
public void test() {
PowerMockito.mockStatic(A.class);
Mockito.when(A.fun()).thenReturn("fun");
PowerMockito.mockStatic(B.class);
Mockito.when(B.fun()).thenReturn("fun");
new A().b();
new B().b();
}
}
a/pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>a</artifactId>
</project>
b/pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>a</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<artifactId>b</artifactId>
</project>
report/pom.xml 用于聚合跨模块的覆盖率:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>report</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>a</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>b</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
最后是最重要的部分 - pom.xml 所有魔法发生在哪里:
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jacoco.version>0.7.9</jacoco.version>
<powermock.version>1.6.6</powermock.version>
</properties>
<modules>
<module>a</module>
<module>b</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>coverage</id>
<modules>
<module>report</module>
</modules>
<dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>${jacoco.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>instrument</id>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>report</id>
<!--
restore original classes before generation of report,
but after packaging of JAR:
-->
<phase>post-integration-test</phase>
<goals>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
在这样的设置中
mvn clean verify -Pcoverage
会生成
-
a/target/site/jacoco/index.html 包含模块 a 中的类的覆盖率,模块 a 中的测试
-
b/target/site/jacoco/index.html 包含模块 b 中的类的覆盖率,模块 b 中的测试
-
report/target/site/jacoco-aggregate/index.html 包含模块 a 和 b 模块中的类的覆盖率 a 和 b 模块中的测试
但是生成的 JAR 文件包含检测类,但是
mvn package -DskiptTests -Dmaven.jar.forceCreation
将生成非仪器化的 JAR。
如果您摆脱 PowerMockito,则不需要这种复杂性。在这种情况下,pom.xml 中的 coverage 配置文件可以简化为:
<profile>
<id>coverage</id>
<modules>
<module>report</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
和mvn clean verify -Pcoverage 将生成不需要重建的相同报告和 JAR。