This question already has answers here:
Surefire is not picking up Junit 5 tests
(19 answers)
Closed 6 years ago.
Maven execution
mvn clean test
I am trying to use junit5 for one of my maven projects but not able to execute the unit tests during the test phase using -
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M3</version>
</dependency>
The output that I get is -
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) # utils ---
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Tried implementing the solution mentioned # Surefire is not picking up Junit 5 tests to update the dependencies to -
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M3</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-api</artifactId>
<version>5.0.0-ALPHA</version><!--https://mvnrepository.com/artifact/org.junit/junit5-api -->
<scope>test</scope>
</dependency>
and updating the plugin to -
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>surefire-junit5</artifactId>
<version>5.0.0-ALPHA</version><!--couldn't find this on the central though-->
</dependency>
</dependencies>
</plugin>
but the output remains same.
Question - Is this custom provider no more supported or is there any solution to executing the tests using maven, junit5 and/or junit5-api currently?
Note - The test execution was working fine with JUnit-4.
You should configure the maven-surefire-plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M3</version>
</dependency>
</dependencies>
</plugin>
You only need to include the junit-jupiter-api artifact and only in test scope in your dependencies section:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M3</version>
<scope>test</scope>
</dependency>
Please see http://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven for more information.
Edit - From the same docs.
In order to have Maven Surefire run any tests at all, a TestEngine
implementation must be added to the runtime classpath.
<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-M3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M3</version>
</dependency>
</dependencies>
</plugin>
Related
I have cucumber framework and the following pom
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>CucumberE2ETest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
After I run my tests, I get this
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
As you can see the Tests run are 0.
The tests run fine from IntelliJ but maven does not run the tests.
My apologies for the large amount of copy-pasted content, but I want to provide as much information as possible because I'm not sure what to try next.
You're using cucumber-junit which integrates with JUnit 4. However because you're using JUnit 5, Surefire will not run JUnit 4 tests. You need to add the JUnit Vintage engine or Cucumber's JUnit 5 integration.
See for a working example:
https://github.com/cucumber/cucumber-java-skeleton
The Maven Failsafe plugin won't find my JUnit 5 integration tests when I'm running the command mvn clean failsafe:integration-test, although it can find the files.
I have the junit-jupiter-api and junit-jupiter-engine as test dependencies:
<properties>
<junit.jupiter.version>5.0.1</junit.jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
My integration tests are named correctly (following the **/*IT.java, **/IT*.java, or the **/*ITCase.java that included by default by Failsafe and excluded by default by Surefire).
Is there any way that I can use JUnit 5 tests with Failsafe?
Edit: This answer was correct before maven-failsafe-plugin:2.22.0. See davidxxx's answer for the ideal and most up to date solution.
The maven-failsafe-plugin currently doesn't support JUnit 5, out of the box.
However, like with maven-surefire-plugin, you can run JUnit 5 tests with the maven-failsafe-plugin by specifying the dependency on the org.junit.platform:junit-platform-surefire-provider:1.0.1 with the earlier version of the maven-failsafe-plugin:2.19.1.
It doesn't work with the current version 2.20 of the failsafe (in the same way that the surefire has the error) due to an OutOfMemory error.
See the below for an example of the configuration of the plugin:
<properties>
<junit.platform.version>1.0.1</junit.platform.version>
</properties>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
</plugin>
You can find a full example of this working (and a failing one) on GitHub. To test that it works, you can run mvn clean failsafe:integration-test.
Note that from the JUnit 5 documentation : junit-platform-surefire-provider should be not used any longer :
Due to the release of Surefire 2.22.0, the
junit-platform-surefire-provider from the JUnit team has been
deprecated and will be discontinued in a subsequent release of the
JUnit Platform.
Additionally, you can also read in the maven-surefire-plugin documentation :
Using JUnit 5 Platform
To get started with JUnit Platform, you need to add at least a single
TestEngine implementation to your project. For example, if you want to
write tests with Jupiter, add the test artifact junit-jupiter-engine
to the dependencies in POM
So you have to specify this test dependency :
<properties>
<junit-jupiter.version>5.2.0</junit-jupiter.version>
</properties>
<dependencies>
[...]
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
And the maven-failsafe-plugin declaration could be as simple as :
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
When I run my test in Maven I get this:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
My test class, JsonReaderTest.class, is placed in src/test/java and follows the correct name convention as far as I know from maven-surefire-plugin.
Tests run fine when run outside of Maven.
I have this plugin included in my pom:
<!-- Executes tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
and this in my dependencies:
<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0</version>
</dependency>
and my test class:
package org.avalin.optaplanner.test.java;
import org.avalin.optaplanner.json.JsonReader;
import org.junit.jupiter.api.*;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.*;
public class JsonReaderTest
{
#Test
#DisplayName("Test: No such file at designated path")
void testloadFromJsonTest() throws Exception
{
Throwable exception = assertThrows(FileNotFoundException.class,
()-> JsonReader.loadFromJson(".json"));
assertEquals(".json (No such file or directory)",
exception.getMessage());
}
#Test
#DisplayName("Test: Load Shifts from JSON (String instead of number)")
void testLoadShiftsFromJson3()
{
Throwable exception = assertThrows(NumberFormatException.class, ()-> JsonReader.loadFromJson(Paths.get("src/main/resources/org/avalin/optaplanner/json/faultyShift-2.json").toAbsolutePath().toString()));
assertEquals("\nOne or more of your \"shift\" elements has a number format exception.\n" +
"Check for errors in your JSON-properties.\n" +
"(Did you insert a string instead of a number in id?)",
exception.getMessage());
}
#Test
#DisplayName("Test: JSON is correctly loaded")
void testJsonIsLoaded()
{
assertFalse(JsonReader.jsonIsLoaded());
}
#AfterEach
void cleanJsonReader()
{
JsonReader.cleanJsonReader();
}
}
When I tried googling this problem, it seemed the only thing that could be wrong would be naming convention (class had to end with or start with test, I tested both with no change) and that the test class should be put into the appropriate folder.
When I run: mvn -Dtest=JsonReaderTest test
I get following:
Failed to execute goal org.apache.maven.plugins:maven-surefire-
plugin:2.20.1:test (default-test) on project optaplanner: No tests were
executed!
The JsonReaderTest.class is also correctly generated inside target/test-classes
What could be the culprit here?
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>
...
This plugin worked for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.0.0-M3</version>
</dependency>
</dependencies>
</plugin>
Taken from https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html
the following pom configuration worked for me:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
....
<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>
...
the plugin part as #glytching stated above
I was having the same problem. All the test classes were package private and JUnit 5 couldn't see them. The solution was to add this dependency to the pom file:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
I had the same issue, it took me days to find the solution!
First, make sure you end your class-name with ***Test.java, for example: MyGreatTest.java
Secondly, make your class and methods public! This is very important, otherwise mvn test will ignore your test class!
Thirdly, I added this peace of code in the pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
along with all junit dependencies like engine, api, launcher this one integrated all I guess
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.9.0-M1</version>
<scope>test</scope>
with latest version of surefire plugin
and it works solely as well for me without other junite dependencies
When I run in Eclipse, Maven and TestNG work fine and pass, but when I run in Jenkins, the following failure message appears:
MojoFailureException
Unlike many other errors, this exception is not generated by the Maven core itself but by a plugin. As a rule of thumb, plugins use this error to signal a failure of the build because there is something wrong with the dependencies or sources of a project, e.g. a compilation or a test failure.
The concrete meaning of the exception depends on the plugin so please have a look at its documentation. The documentation for many common Maven plugins can be reached via our plugin index.
This is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.techbeamers</groupId>
<artifactId>loadtesting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Load Testing</name>
<description>Selenium Load Testing Example using TestNG and Maven</description>
<properties>
<selenium.version>2.53.1</selenium.version>
<testng.version>6.9.10</testng.version>
</properties>
<build>
<plugins>
<!-- Below plug-in is used to execute tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<!-- TestNG suite XML files -->
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<!-- Include the following dependencies -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
</project>
Please help -- I don't know what's wrong with my plugin or dependencies.
The first possible problem I see: you define the property: testng.version, set the value to: 6.9.10, but then ignore the testng.version property later in your pom.xml, and set the <version> of TestNG to: 6.8.
Try changing the definition of your TestNG <dependency> from what you curently have:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
So that the definition is defined as follows:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
I got following execption when i tried to run test case in junit5:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test (default-test) on project CRUD-App: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: There was an error in the forked process
org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
at org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:161)
at org.junit.platform.launcher.core.DefaultLauncher.<init>(DefaultLauncher.java:52)
at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42)
at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:59)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:286)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:240)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
pom.xml
<dependencies>
...
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-api</artifactId>
<version>5.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
<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-M2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Test Class:
public class PersonServiceTest {
final Database database = Database.from("jdbc:h2:mem:" + App.DB_NAME);
final PersonService personService = new PersonService(database);
public PersonServiceTest() {
}
#Test
#DisplayName("#Person#insert()")
public void testInsert() {
personService.insert(new PersonBuilder()
.setId(1).setName("Bhuwan")
.setAddress("KTM")
.setContactNo("984849").createPerson()
);
}
}
Maven Goal: mvn test
Mixing ALPHA snapshot artifacts (i.e., org.junit:junit5-api:5.0.0-SNAPSHOT) with M2 artifacts (i.e., org.junit.platform:junit-platform-surefire-provider:1.0.0-M2), won't work.
The Maven section in the user guide suggests to check out the pom.xml from the junit5-maven-consumer project. If you follow that example, you will end up with something like the following.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
<junit.platform.version>1.0.0-M2</junit.platform.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
To write your tests, you only need the junit-jupiter-api; however, in order to run your tests you must have a TestEngine on the classpath. For JUnit Jupiter you therefore need junit-jupiter-engine on the classpath as well.
As Nicolai Parlog pointed out, you could add junit-jupiter-engine as a dependency for the maven-surefire-plugin; however, that would not include the JupiterTestEngine in the classpath for your IDE.
If you're only running tests via Maven or with a recent beta version of IntelliJ 2016 (which has built-in support for JUnit 5), then you may not care if JupiterTestEngine is on the classpath in your IDE. But... if you're using Eclipse, NetBeans, or a non-beta version of IntelliJ, you'll definitely want the JupiterTestEngine on the classpath in the IDE as well.
Regards,
Sam (core JUnit 5 committer)
Ended up here since getting the same error using Gradle and JUnit 4. The solution in this case can be found in the docs:
The JUnit Platform can run JUnit 4 based tests as long as you
configure a testImplementation dependency on JUnit 4 and a
testRuntimeOnly dependency on the JUnit Vintage TestEngine
implementation similar to the following.
A test engine is also needed:
dependencies {
testImplementation("junit:junit:4.13.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.8.2")
}
The Maven Surefire plugin not only needs the JUnit 5 provider but also a TestEngine implementation to run tests with. To quote the JUnit 5 docs:
In order to have Maven Surefire run any tests at all, a TestEngine
implementation must be added to the runtime classpath.
In accordance with that the following works:
<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-M4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M4</version>
<scope>test</scope>
</dependency>
</dependencies>
Note that this configuration makes the engine a dependency of the surefire plugin, not of your test code.
simply adding below dependency solved this prob.
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
I solve it changing
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.8.RELEASE"
to
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.2.0.RELEASE"