0
0
JunitHow-ToBeginner ยท 4 min read

How to Generate Test Report in JUnit Easily

To generate a test report in JUnit, run your tests using a build tool like Maven or Gradle which automatically creates XML reports. Alternatively, use IDEs or CI tools that parse these reports to show test results clearly.
๐Ÿ“

Syntax

JUnit itself runs tests and produces results, but test reports are generated by tools that run JUnit tests. Commonly, Maven or Gradle are used to run tests and generate reports in XML or HTML format.

Example Maven command to run tests and generate reports:

  • mvn test - runs tests and creates reports in target/surefire-reports

Example Gradle command:

  • gradle test - runs tests and generates reports in build/reports/tests/test
bash
mvn test

gradle test
๐Ÿ’ป

Example

This example shows a simple JUnit 5 test class and how to run it with Maven to generate a test report.

The report will be saved automatically by Maven in target/surefire-reports as XML and plain text files.

java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {

    @Test
    void addition() {
        assertEquals(5, 2 + 3);
    }

    @Test
    void subtraction() {
        assertEquals(1, 3 - 2);
    }
}
Output
------------------------------------------------------- T E S T S ------------------------------------------------------- Running CalculatorTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
โš ๏ธ

Common Pitfalls

  • Not running tests via build tools: Running tests directly in IDE may not generate reports automatically.
  • Missing plugins: Maven requires maven-surefire-plugin to run tests and generate reports.
  • Report location confusion: Reports are usually in target/surefire-reports (Maven) or build/reports/tests/test (Gradle).
  • JUnit version mismatch: Use JUnit 5 dependencies and plugins compatible with your build tool.
xml
/* Wrong: Running tests without Maven or Gradle - no reports generated */

/* Right: Use Maven with surefire plugin configured in pom.xml */

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M7</version>
      </plugin>
    </plugins>
  </build>
</project>
๐Ÿ“Š

Quick Reference

CommandDescriptionReport Location
mvn testRun tests and generate reports with Maventarget/surefire-reports
gradle testRun tests and generate reports with Gradlebuild/reports/tests/test
IDE runRun tests in IDE (may not generate reports)Depends on IDE settings
โœ…

Key Takeaways

Use Maven or Gradle to run JUnit tests and automatically generate test reports.
Test reports are saved as XML and HTML files in standard build directories.
Ensure build plugins like maven-surefire-plugin are configured for report generation.
Running tests only in IDE may not produce test reports unless configured.
Check report folders after test runs to review detailed test results.