完整代码Github地址: https://github.com/mgljava/jacoco-demo
Maven:Apache Maven是一个软件项目管理和理解工具。Maven基于项目对象模型(POM)的概念,可以从中心信息管理项目的构建、报告和文档
Jacoco:Java Code Coverage Library
1.创建Maven工程--》不多说。我用的是Intellij IDEA IDE
2.配置maven-compiler-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<skipMain>true</skipMain>
<skip>true</skip>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
3.配置Jacoco-maven-plugins
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/jacoco-report</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
4.编写业务代码,这里用作举例,就弄一个简单的计算类
package com.github.mgljava.calc;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int sub(int a, int b) {
return a - b;
}
public int multi(int a, int b) {
return a * b;
}
public int div(int a, int b) {
return a / b;
}
public int switchCase(int a) {
switch (a) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
return 4;
}
}
}
5.编写测试类
package com.github.mgljava.calc;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
private Calculator calculator = new Calculator();
@Test
public void should_get_30_when_a_equal_20_and_b_equal_10() {
// given
int a = 10;
int b = 20;
// when
final int addNumber = calculator.add(a, b);
// then
assertEquals(30, addNumber);
}
@Test
public void should_get_18_when_a_equal_20_and_b_equal_2() {
// given
int a = 20;
int b = 2;
// when
final int subNumber = calculator.sub(a, b);
// then
assertEquals(18, subNumber);
}
@Test
public void should_get_6_when_a_equal_3_and_b_equal_2() {
// given
int a = 3;
int b = 2;
// when
final int multiResult = calculator.multi(a, b);
// then
assertEquals(6, multiResult);
}
@Test
public void should_get_5_when_a_equal_10_and_b_equal_2() {
// given
int a = 10;
int b = 2;
// when
final int divResult = calculator.div(a, b);
// then
assertEquals(5, divResult);
}
@Test
public void should_get_1_when_arg_is_1() {
// given
int a = 1;
// when
final int result = calculator.switchCase(a);
// then
assertEquals(1, result);
}
@Test
public void should_get_2_when_arg_is_2() {
// given
int a = 2;
// when
final int result = calculator.switchCase(a);
// then
assertEquals(2, result);
}
@Test
public void should_get_3_when_arg_is_3() {
// given
int a = 3;
// when
final int result = calculator.switchCase(a);
// then
assertEquals(3, result);
}
@Test
public void should_get_4_when_arg_is_4() {
// given
int a = 4;
// when
final int result = calculator.switchCase(a);
// then
assertEquals(4, result);
}
}
6.运行命令:mvn test ,可以在下图中看到生成的测试报告目录
7.可以在浏览器中看到测试报告: