[JAVA] Output JUnit test report in Maven

I summarized the procedure to execute JUnit test in Maven and output the test report (HTML).

(1) Add plugin settings to pom.xml

<project ...>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
          <useSystemClassLoader>false</useSystemClassLoader>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.7.1</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.22.1</version>
      </plugin>
    </plugins>
  </build>
  ...
</project>

The maven-surefire-plugin configuration is required to avoid problems.

Reference: https://qiita.com/watanabk/items/16e19e30659d0acca519

There is no problem even if you do not list other plugins. (If not stated, the latest version will be used.)

(2) Execute test and generate report with mvn command

mvn clean \
    test -Dmaven.test.failure.ignore=true \
    site -DgenerateReports=false \
    surefire-report:report

The test is run and the test result report is output to target / site / surefire-report.html.

The role of each phase or goal is as follows.

clean (Phase)

Delete the build result (target).

test (Phase)

Run the test. Specify -Dmaven.test.failure.ignore = true to output the report even if there is a failed test.

site (Phase)

Originally a build life cycle for outputting the project site. Since the image and CSS linked to the report are not output only by executing surefire-report: report, execute by specifying -DgenerateReports = false and output only the image and CSS.

Reference: https://stackoverflow.com/questions/21432663/how-to-get-the-icons-for-the-resulted-maven-surefire-report-plugin

surefire-report:report (Goal)

Output a report (HTML) from the JUnit execution result (XML) in target / surefire-report.

Reference: https://maven.apache.org/surefire/maven-surefire-report-plugin/usage.html

Reference: http://maven.apache.org/surefire/maven-surefire-report-plugin/faq.

Recommended Posts

Output JUnit test report in Maven
Test private methods in JUnit
How to filter JUnit Test in Gradle
Control test order in Junit4 with enumeration
Refactoring in JUnit
[CircleCI 2.0] [Java] [Maven] [JUnit] Aggregate JUnit test results with CircleCI 2.0
Java automated test implementation with JUnit 5 + Apache Maven
JUnit 5: How to write test cases in enum
Run JUnit and Spock in a maven project
Try JUnit test launch
Get the name of the test case in the JUnit test class
Implement test doubles in JUnit without using external libraries
[Test code learning / output]
Reload dependencies in Maven
Output triangle in Ruby
Unit test with Junit.
Output in multiples of 3
[Java] I want to test standard input & standard output with JUnit
[Java] JUnit4 test case example
Test Web API with junit
About validation methods in JUnit
[Spring] Controller exception output test
[JUnit] Test the thrown exception
Supports 0 drop in CSV output
[Spring Boot] Until @Autowired is run in the test class [JUnit5]