JaCoCo helps you see how much of your code is tested. It shows which parts are covered by tests and which are not.
JaCoCo setup and configuration in JUnit
plugins {
id 'java'
id 'jacoco'
}
jacoco {
toolVersion = '0.8.10'
}
test {
useJUnitPlatform()
finalizedBy jacocoTestReport
}
jacocoTestReport {
dependsOn test
reports {
xml.required.set(true)
html.required.set(true)
}
}This example is for a Gradle build file (build.gradle).
Make sure to use the correct JaCoCo version compatible with your setup.
plugins {
id 'java'
id 'jacoco'
}
jacoco {
toolVersion = '0.8.10'
}test {
useJUnitPlatform()
finalizedBy jacocoTestReport
}jacocoTestReport {
dependsOn test
reports {
xml.required.set(true)
html.required.set(true)
}
}This Gradle build file sets up JaCoCo with JUnit tests. The CalculatorTest checks the add method. After tests run, JaCoCo creates coverage reports in XML and HTML formats.
plugins {
id 'java'
id 'jacoco'
}
jacoco {
toolVersion = '0.8.10'
}
test {
useJUnitPlatform()
finalizedBy jacocoTestReport
}
jacocoTestReport {
dependsOn test
reports {
xml.required.set(true)
html.required.set(true)
}
}
// Sample JUnit test class
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CalculatorTest {
@Test
void testAdd() {
Calculator calc = new Calculator();
assertEquals(5, calc.add(2, 3));
}
}
// Calculator class
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}Run the Gradle command ./gradlew test jacocoTestReport to execute tests and generate reports.
Check the HTML report in build/reports/jacoco/test/html/index.html to see coverage visually.
Keep your JaCoCo version updated for best compatibility and features.
JaCoCo shows which code your tests cover.
Set it up in your build tool to generate reports automatically.
Use reports to improve your tests and code quality.
Practice
Solution
Step 1: Understand JaCoCo's role
JaCoCo is a code coverage tool that tracks which parts of your code are executed during tests.Step 2: Compare options
Only To measure how much of your code is tested by your unit tests correctly describes JaCoCo's purpose; others describe unrelated functions.Final Answer:
To measure how much of your code is tested by your unit tests -> Option DQuick Check:
JaCoCo = Code coverage tool [OK]
- Confusing JaCoCo with test runners
- Thinking JaCoCo generates tests
- Assuming JaCoCo replaces JUnit
pom.xml file?Solution
Step 1: Identify JaCoCo plugin syntax
JaCoCo is added as a Maven plugin with groupId 'org.jacoco' and artifactId 'jacoco-maven-plugin'.Step 2: Eliminate incorrect options
Options B and D are unrelated plugins; A is a dependency, not a plugin.Final Answer:
Correct JaCoCo plugin configuration in Maven -> Option AQuick Check:
JaCoCo plugin = org.jacoco:jacoco-maven-plugin [OK]
- Adding JaCoCo as a dependency instead of plugin
- Confusing JUnit plugin with JaCoCo plugin
- Using unrelated Maven plugins
pom.xml:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
What will happen when you run mvn test?Solution
Step 1: Understand prepare-agent goal
The 'prepare-agent' goal sets up JaCoCo to collect coverage data during test execution.Step 2: Understand report goal in test phase
The 'report' goal runs after tests to generate the coverage report.Final Answer:
JaCoCo collects coverage during tests and generates report after -> Option AQuick Check:
prepare-agent + report = coverage data + report [OK]
- Assuming coverage is collected without prepare-agent
- Thinking report runs before tests
- Confusing goals causing build failure
mvn test. What is the most likely cause?Solution
Step 1: Check JaCoCo execution bindings
Without binding the 'report' goal to the 'test' phase, JaCoCo won't generate reports automatically.Step 2: Eliminate other causes
Incorrect version usually causes build errors; test correctness or compiler plugin absence won't stop report generation if configured.Final Answer:
Missing binding of report goal to test phase -> Option CQuick Check:
Report goal must be bound to test phase [OK]
- Ignoring execution phase bindings
- Blaming test code for missing reports
- Assuming version mismatch causes silent failure
build.gradle?Solution
Step 1: Identify correct JaCoCo Gradle report syntax
The 'jacocoTestReport' task configures reports with 'xml.enabled' and 'html.enabled' properties.Step 2: Compare options for XML report generation
jacocoTestReport { reports { xml.enabled = true html.enabled = true } } correctly enables XML report; the snippet using 'required' properties is outdated; others configure in wrong blocks.Final Answer:
jacocoTestReport with reports { xml.enabled = true } -> Option BQuick Check:
Use xml.enabled = true in jacocoTestReport [OK]
- Using xml.required instead of xml.enabled
- Configuring jacoco block instead of jacocoTestReport
- Missing enabling of reports
Ready to prove it?
Pass the test and this pattern moves unknown
