While running through Run As-> Junit executing Junit test cases but when run through Maven test throwing below error.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.916 s
[INFO] Finished at: 2021-05-08T04:50:23+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project maven-junit-code-coverage-Jacoco: There are test failures.
[ERROR]
[ERROR] Please refer to D:\Workspace\maven-junit-code-coverage-Jacoco\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Problem
In maven java project, maven-surefire-plugin is required for any types of reports generation because mvn test command execute the test cases but there is not added the plug in for maven-surefire-plugin that’s why maven is not able to execute test cases.
Solution
To execute your test cases with Junit in maven Java project, you can add Junit dependency with maven-surefire-plugin.
JUnit 5 Dependency
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
</dependency>
</dependencies>
Maven Surefire Report Plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
I have tested this solution with Junit 5 and working perfectly fine for me. You can change this dependency in case using Junit 4.
I have solved this problem by using above solution. If you know any other way to solve it write in comments to help others.