Maven does not find JUnit tests to run
By default Maven uses the following naming conventions when looking for tests to run:
Test*
*Test
*Tests
(has been added in Maven Surefire Plugin 2.20)*TestCase
If your test class doesn't follow these conventions you should rename it or configure Maven Surefire Plugin to use another pattern for test classes.
Maven not running JUnit 5 tests
According to the annotation (import org.junit.jupiter.api.Test
), you are trying to run JUnit 5 tests with Maven. According to the documentation, you have to add this dependency:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
Your version of Maven comes with a version of maven-surefire-plugin
which does not support JUnit 5. You could update your Maven to the latest version. You could also set the version of the maven-surefire-plugin
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<version>2.22.0</version>
</plugin>
See the junit5-samples for this information.
See the Maven Surefire Plugin artifact in a Maven repository. At version 3.0.0-M3
as of 2019-01.
Maven doesn't find JUnit test to run
The are several issues. First compiler source/target 1.6 does not work cause Junit 5 needs JDK 8 minimum...
To run JUnit 5 tests you have to add the following dependency:
<dependencies>
[...]
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
Second as already mentioned you need to pin maven-surefire-plugin to minimum 2.22.1...
Maven not running JUnit test
If you're using JUnit 5 (Jupiter), you need to import org.junit.jupiter.api.Test
instead of org.junit.Test
(the latter is for JUnit 4).
Maven silently fails to find JUnit tests to run
It was an issue with the maven-surefire-plugin
There was something wrong with the default version of the maven-surefire-plugin
, and I was able to fix it by upgrading that. I solved the problem by copying relevant sections from the JUnit5 sample Maven project on Github:
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
Tests not running through Maven?
Using the Maven Surefire plugin and JUnit 5 together requires some tweaking ...
From the docs:
The JUnit team has developed a very basic provider for Maven Surefire that lets you run JUnit 4 and JUnit Jupiter tests via mvn test. The pom.xml file in the junit5-maven-consumer project demonstrates how to use it and can serve as a starting point.
Due to a memory leak in Surefire 2.20, the junit-platform-surefire-provider currently only works with Surefire 2.19.1.
...
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
...
Maven will not find junit tests to execute, says it ran 0 tests and the build succeeded
org.junit.jupiter.api.Test
is junit 5 (which may not be supported fully yet), and not junit 4. Remove the jupiter dependency and use junit 4 for now. A sample jUnit 4 test from https://github.com/junit-team/junit4/wiki/getting-started:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
@Test
public void evaluatesExpression() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
assertEquals(6, sum);
}
}
Related Topics
Spring Boot and Multiple External Configuration Files
How to Specify the Java Compiler Version in a Pom.Xml File
How to Programmatically Download a Webpage in Java
Why Should a Java Class Implement Comparable
Using Regex to Generate Strings Rather Than Match Them
How to Use a Custom Serializer with Jackson
Map Implementation with Duplicate Keys
Initialising a Multidimensional Array in Java
Convert Date/Time for Given Timezone - Java
String, Stringbuffer, and Stringbuilder
What Is Suppresswarnings ("Unchecked") in Java
Dynamically Add Components to a Jdialog
Jsoup Java HTML Parser:Executing JavaScript Events
How to Deploy a Javafx 11 Desktop Application with a Jre
What Is Variable Shadowing Used for in a Java Class