JaCoCo , , . , , , , , , .
Maven , maven-surefire-plugin
, option test
. :
src/main/java/Example.java
:
public class Example {
public void doSomething(int p) {
if (p == 1) {
a();
} else {
b();
}
}
private void a() {
System.out.println("a");
}
private void b() {
System.out.println("b");
}
}
src/test/java/ExampleTest.java
:
import org.junit.Test;
public class ExampleTest {
@Test
public void test1() {
new Example().doSomething(1);
}
@Test
public void test2() {
new Example().doSomething(2);
}
}
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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
mvn clean verify -Dtest=ExampleTest#test1
target/site/jacoco
:

mvn clean verify -Dtest=ExampleTest#test2
:

test1
test2
.
- mvn clean verify
:

clean
: target/jacoco.exec
(. destfile
jacoco-maven-plugin
). JaCoCo (. append
jacoco-maven-plugin
), clean
, .
Gradle, - build.gradle
:
apply plugin: 'java'
apply plugin: 'jacoco'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
gradle clean test --tests ExampleTest.test1 jacocoTestReport
, test1
, , Maven.
Maven clean
, build/jacoco/test.exec
- . append
JaCoCo Gradle Plugin
.
Eclipse IDE, EclEmma Eclipse, JaCoCo Eclipse IDE "Eclipse IDE Java", Oxygen (4.7). Eclipse IDE - , , "Coverage As → JUnit Test".