Skip to content

Latest commit

 

History

History
242 lines (178 loc) · 6.72 KB

File metadata and controls

242 lines (178 loc) · 6.72 KB

JUnit 5 & JaCoCo Setup (IntelliJ + Maven)

This guide ensures everyone has tests (JUnit 5) and coverage (JaCoCo) working the same way.

TL;DR: Pull the repo and run Maven → verify in IntelliJ.
JUnit 5 and JaCoCo are configured in pom.xml. No extra IntelliJ setup needed.


1) Prerequisites

  • IntelliJ IDEA 2025.x (Community or Ultimate)
  • JDK 17 or 21 (Temurin recommended)
  • Maven
    • You can use IntelliJ’s built-in Maven (no CLI install required), or the wrapper if present (mvnw/mvnw.cmd).

Verify JDK in IntelliJ:

  • File → Settings → Build, Execution, Deployment → Build Tools → Maven
    • Maven home: Bundled (or your installed Maven)
    • JDK for importer: your project JDK (17/21)

2) What’s already in pom.xml

JUnit 5 dependency + Surefire + JaCoCo are already configured.
If you’re copying into another project, ensure these blocks exist.

Dependencies (JUnit 5):

<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
  </dependency>
</dependencies>

Build plugins (Surefire + JaCoCo):

<build>
  <plugins>

    <!-- JaCoCo: attach agent before tests, generate report after -->
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.12</version>
      <executions>
        <execution>
          <goals><goal>prepare-agent</goal></goals>
        </execution>
        <!-- Report after tests (verify phase is robust across IDEs/CI) -->
        <execution>
          <id>report</id>
          <phase>verify</phase>
          <goals><goal>report</goal></goals>
        </execution>
        <!-- Optional: enforce minimum coverage -->
        <!--
        <execution>
          <id>check</id>
          <goals><goal>check</goal></goals>
          <configuration>
            <rules>
              <rule>
                <element>BUNDLE</element>
                <limits>
                  <limit>
                    <counter>LINE</counter>
                    <value>COVEREDRATIO</value>
                    <minimum>0.80</minimum>
                  </limit>
                </limits>
              </rule>
            </rules>
          </configuration>
        </execution>
        -->
      </executions>
    </plugin>

    <!-- Surefire: runs JUnit 5 tests & propagates JaCoCo agent args -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.2.5</version>
      <configuration>
        <argLine>${argLine}</argLine>
        <useModulePath>false</useModulePath>
      </configuration>
    </plugin>

  </plugins>
</build>

3) Where tests go (folder & package)

Test source root: src/test/java

Mirror the package structure of src/main/java.

Example:

src/main/java/com/group5/app/Main.java
src/test/java/com/group5/app/MainTest.java

Tip: In IntelliJ, if src/test/java isn’t marked as a Test Sources Root, right-click it → Mark Directory As → Test Sources Root.


4) How to run tests & generate the JaCoCo HTML report (IntelliJ GUI)

Open the Maven tool window.

Run Lifecycle → clean, then Lifecycle → verify.

After “BUILD SUCCESS” open the HTML report:

target/site/jacoco/index.html

This is the coverage artifact you can screenshot/attach for submission (the brief expects an automated check with JaCoCo and 80%+ coverage).

5) Using IntelliJ’s “Run with Coverage” (Optional during development)

Note: This is optional. Intellij offers a plugin Code Coverage that can be installed from the IntelliJ Marketplace. https://plugins.jetbrains.com/plugin/8554-code-coverage

For quick, visual feedback while coding:

Right-click a test/class → More Run/Debug → Run … with Coverage.

You’ll see coverage % in the editor and a Coverage tool window.

IntelliJ’s coverage is great for day-to-day feedback. The official report for submission/CI is the JaCoCo HTML generated by Maven.

6) Quick verification checklist

mvn test or Maven → Lifecycle → test runs at least one test.

Maven log shows:

jacoco-maven-plugin:0.8.12:prepare-agent

maven-surefire-plugin:3.2.x:test

jacoco-maven-plugin:0.8.12:report

target/site/jacoco/index.html opens with coverage tables & class listings.

Coverage is ≥ 80% for core classes (target per project brief).

7) Common issues & fast fixes

“Skipping JaCoCo execution due to missing execution data file”

You ran only IntelliJ coverage (no Maven) → run clean → verify in Maven.

Tests were skipped (-DskipTests=true) → run without skip flags.

Surefire didn’t run tests → ensure you have at least one JUnit 5 test and the Surefire plugin is present.

IntelliJ can’t find tests / “no tests found”

Test class is in the wrong folder (must be under src/test/java).

Package mismatch: the package line must match the folder path.

Mark src/test/java as Test Sources Root.

ClassNotFound when running a test

The Run Configuration package declaration is missing. Edit it to use fully qualified class name, e.g., com.group5.app.MainTest.

IntelliJ shows red underlines on the JaCoCo plugin

Click the Maven tool window’s Reload/Sync button.

Settings → Maven → Repositories → Update (background).

Worst case: File → Invalidate Caches & Restart.

8) (Optional) Continuous Integration (GitHub Actions)

If enabled, every push/PR will run tests and upload the JaCoCo report:

name: Maven CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
- run: mvn -q -B clean verify
- name: Upload JaCoCo report
if: always()
uses: actions/upload-artifact@v4
with:
name: jacoco-report
path: target/site/jacoco/

If you uncomment the JaCoCo check rule in pom.xml, CI will fail when coverage is below the threshold (e.g., 80%).

9) Minimal sample test (sanity check)

package com.group5.app;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MainTest {
@Test
void sanity() {
assertTrue(true);
}
}

Run Maven → verify and open target/site/jacoco/index.html. You should see at least minimal coverage recorded.

FAQ

Q: Do I need to “activate” JUnit or JaCoCo in IntelliJ? A: No. With the shared pom.xml, IntelliJ detects dependencies and runs Maven goals. JUnit 5 is used by Surefire; JaCoCo attaches automatically during Maven test/verify.

Q: Can I use IntelliJ’s coverage instead of JaCoCo? A: Use IntelliJ coverage for quick local feedback. For the submission/metrics, use the Maven/JaCoCo HTML report (what assessors/CI expect)