Surefire is not picking up Junit 5 tests - java
I wrote a simple test method with JUnit 5:
public class SimlpeTest {
#Test
#DisplayName("Some description")
void methodName() {
// Testing logic for subject under test
}
}
But when I run mvn test, I got:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running SimlpeTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Somehow, surefire didn't recognize that test class. My pom.xml looks like:
<properties>
<java.version>1.8</java.version>
<junit.version>5.0.0-SNAPSHOT</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
Any idea how to make this work?
The maven-surefire-plugin, as of today, does not have full support of JUnit 5. There is an open issue about adding this support in SUREFIRE-1206.
As such, you need to use a custom provider. One has already been developed by the JUnit team; from the user guide, you need to add the junit-platform-surefire-provider provider and the TestEngine implementation for the new API:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<!-- latest version (2.20.1) does not work well with JUnit5 -->
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Also, be sure to declare the junit-jupiter-api dependency with a scope of test:
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>
Update 2
Issue has been fixed in Maven Surefire Plugin v2.22.0
New version is available at Maven Central Repository.
Maven
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</dependency>
Gradle
compile group: 'org.apache.maven.plugins', name: 'maven-surefire-plugin', version: '2.22.0'
Update
As Marian pointed out, the latest version of JUnit 5 Platform Surefire Provider (1.2.0) supports latest version of Maven Surefire Plugin (2.21.0):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</plugin>
Example
pom.xml
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
TestScenario.java
package test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class TestScenario {
#Test
#DisplayName("Test 2 + 2 = 4")
public void test() {
Assertions.assertEquals(4, 2 + 2);
}
}
Output (mvn clean install)
...
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) # test ---
[INFO] [INFO]
------------------------------------------------------- [INFO] T E S T S [INFO]
------------------------------------------------------- [INFO] Running test.TestScenario [INFO] Tests run: 1, Failures: 0,
Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in test.TestScenario
[INFO] [INFO] Results: [INFO] [INFO] Tests run: 1,
Failures: 0, Errors: 0, Skipped: 0
...
Simplest way till today:
<plugin>
<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.1.0</version>
</dependency>
</dependencies>
</plugin>
From the JUnit 5 documentation :
Starting with version 2.22.0, Maven Surefire provides native support
for executing tests on the JUnit Platform.
Additionally you can 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 just that is enough to make run JUnit 5 tests :
<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>davidxxx</groupId>
<artifactId>minimal-pom-junit5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<junit-jupiter.version>5.2.0</junit-jupiter.version>
<!--optional below but good practice to specify our java version-->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<!--optional below -->
<!-- add any JUnit extension you need such as -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>
On my GitHub space I added a working sample maven project that you can browse/clone.
URL: https://github.com/ebundy/junit5-minimal-maven-project
I encountered the same problem in August 2019 which I asked about here: Maven silently fails to find JUnit tests to run. These answers led me in the right direction, but I found that you can solve the problem even more concisely. I copied my solution from the JUnit5 sample Maven project.
As of JUnit 5.5.1 and maven-surefire-plugin 2.22.2, you do not need to add the junit-platform-surefire-provider dependency. It is enough to have this one dependency and one plugin specified in your pom.xml:
<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>
I ran into this issue with JUnit5 and Maven but also noticed that, even if only junit-jupiter-engine was added as a dependency, tests would run on some projects, not on others. And I kind of see the same pattern in the comments here: In #Alex comment above you can see he doesn't have any issue, even with earlier versions of surefire/junit/platform.
After scratching my head for some time I realized that those projects where the tests wouldn't run were those where the tests method names dit not contain the word "test". Though this isn't mandated by http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
In other words:
just with
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
this
#Test
public void something() {
assertTrue(true);
}
will NOT be run, whereas
#Test
public void testSomething() {
assertTrue(true);
}
WILL be run !
This issue unfolds as a russian doll...
Anyway, +1 for #Mikhail Kholodkov whose updated answer fixes all the issues at once!
Just to complement, surefire 2.22.0 + junit 5.2.0 + platform 1.2.0 also works. Attached is a working pom for your referecne:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jjhome.junit5</groupId>
<artifactId>junit5-hone</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>junit5-home</name>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit5.version>5.2.0</junit5.version>
<platform.version>1.2.0</platform.version>
<surefire.version>2.22.0</surefire.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
In my case this was because of the TestNG in the classpath (SUREFIRE-1527). Groovy 2.5.5 POM have brought it with the groovy-testng module.
Manually specified test-framework provider (as it's described at the https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html) solved the problem:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>2.22.1</version>
</dependency>
One thing I noticed that I was able to get it working:
Naming my test class ClinicCalendarShould does not get picked up by maven
Naming my test class ClinicCalendarTest DOES get picked up by maven
So, unless I am missing some sort of configuration or parameter or whatever in the surefire plugin, by default, you need to name your test classes XXXTest.
I was facing the same issue both junit5 and maven-surefire tests were failing. However junit4 was running fine. Below combination worked for me, I don't add the versioning. Use junit-bom for dependency management. Using spring-boot 2.1.4
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.6.1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
Make sure to upgrade to the latest version of eclipse
Update 2022
The following now works:
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
And of-course the dependency added:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
The tests are now detected.
There is open issue for surefire 2.20
It is working for me with surfire 2.19 + junit-platform-* 1.0.3
I had a similar problem also causing Surefire to recognize zero tests.
My problem turned out to be related to the following (from the JUnit 5.1.0 / maven documentation):
Due to a memory leak in Surefire 2.20 and issues running on Java 9, the junit-platform-surefire-provider currently only works with Surefire 2.19.1.
I was trying to use the latest versions of Surefire (2.21.0) and junit-platform-surefire-provider (1.1.0), and it did not work (in neither Java 8 or 9)
Switching back to Surefire 2.19.1 solved my problem.
According to this issue a fix will be included in version 1.2.0 of the junit-platform-surefire-provider (currently available as SNAPSHOT only).
In my case, the surefire plugin didn't get the correct version auf the jupiter-engine/api. And that was even if running Maven 3.6.1 und surefireplugin Version 2.22.2!
Now my surefire-plugin configuration looks like:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
</dependency>
</dependencies>
</plugin>
...
</plugins>
</pluginManagement>
Further more, I had to force these Versions:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies>
</dependencyManagement>
Looks like 5.5.2 was linked to the wrong platform version 1.3.2 instead of 1.5.2 in my case.
All JUnit5 Tests gets picked up now. Even with 2.22.0 of the surefire plugin this wasn't the case for me!
Hope that helps...
Updating to maven-surefire-plugin:2.20 runs the Junit5 tests with no problem.
But I am using the M6 version on Junit5.
Be careful with nested tests. I had a test class like this:
class AcrTerminalTest {
#Nested
class card_is_absent {
AcrTerminal acrTerminal = new AcrTerminal(new CardAbsentFakeCardTerminal());
#Test
void isCardPresent_returns_false() {
assertThat(acrTerminal.isCardPresent())
.isFalse();
}
}
}
Running just ./mvnw test -Dtest=AcrTerminalTest failed, I needed to add * after test class name like this ./mvnw test -Dtest=AcrTerminalTest\* (see the asterisk).
This was commented by schnell18 some lines above https://stackoverflow.com/a/51796487/1619489
For me was such easy as:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
</parent>
<build>
<finalName>app-console-services</finalName>
<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>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
... And this dependency...
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
Based on my case experience there should be no dependency on library junit-jupiter-engine in pom.xml. Then one can use plugin maven-surefire-plugin and dependency on junit-jupiter of the newest version
I ran into a similar problem when upgrading from junit 4 to 5.
from
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
to
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
I used an Eclipse feature to create the unit test class. Right click the class file to test --> New --> Other --> Junit Test Case --> New JUnit Jupiter test. This will nicely create a stubbed out Unit Test class. Beware however as the stubbed out Unit Test class will NOT have the public class identifier nor will the corresponding annotated test method(s) be public.
The unit test will run fine in Eclipse but when run from bash, 'mvn test', the corresponding test will not be detected (no warning either).
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
If you revert to junit 4, 'mvn test' will warn you.
Tests in error:
initializationError(com.mycompany.operations.ConvertExcelTest): The class com.mycompany.operations.ConvertExcelTest is not public.
initializationError(com.mycompany.operations.ConvertExcelTest): Test class should have exactly one public constructor
initializationError(com.mycompany.operations.ConvertExcelTest): Method testExecute_excel97File() should be public
Tests run: 3, Failures: 0, Errors: 3, Skipped: 0
The solution for me was to make the corresponding test class public and to make the test method public too.
public class ConvertExcelTest {
#Test
public void testExecute_excel97File() {
For me the solution is adding the annotation below on top of the class.
#RunWith(JUnitPlatform.class)
public class MyTest {
....
}
Then even the surefire plugin is not required.
Related
Using PowerMock in JDK 16
Java reflection is becoming more and more restricted: Up to Java 8 all operations are allowed Starting from Java 9 to 15 you are still able to perform the operations, but you will receive a warning From Java 16 and onwards the operations are forbidden between modules (well, still possible with some special arguments passed to the JVM) This is a serious problem when using libraries that rely heavily on reflection, like PowerMock that uses it to mock objects in tests. I created this simple example that illustrates the issue. Here is the pom.xml: <?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Powermock</name> <properties> <java.version>16</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.7.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.9</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.9</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <parameters>true</parameters> </configuration> </plugin> </plugins> </build> </project> With this simple test SomeTest.java: package com.example.demo; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.powermock.modules.junit4.PowerMockRunner; #RunWith(PowerMockRunner.class) class SomeTest { #Test void contextLoads() { Assert.assertEquals(1, 1); } } Then if we run the command mvn clean test we will get the error: Running com.example.demo.SomeTest Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.341 sec <<< FAILURE! initializationError(com.example.demo.SomeTest) Time elapsed: 0 sec <<< ERROR! java.lang.RuntimeException: java.lang.reflect.InaccessibleObjectException: Unable to make protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException accessible: module java.base does not "opens java.lang" to unnamed module #8b96fde Strangely if we add this bit to the pom.xml, the problem goes away: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.1</version> </parent> Anybody that can help me with this: How to use PowerMock in JDK 16? Is even PowerMock supported in JDK 16? Is this issue solved using Java modules? Why adding Spring Boot as parent solves the problem?
You can use PowerMockito with Java 16 by using the --illegal-access=permit option in the Maven surefire plugin. You will see warnings, but it works. <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <argLine> --illegal-access=permit </argLine> </configuration> </plugin> For reference this is what I use for Mockito and PowerMockito: <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.11.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.9</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.9</version> <scope>test</scope> </dependency>
Use This plugin in POM file: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine> --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.base=ALL-UNNAMED </argLine> </configuration> </plugin>
Why can't Maven run tests but VSCode can? [duplicate]
I wrote a simple test method with JUnit 5: public class SimlpeTest { #Test #DisplayName("Some description") void methodName() { // Testing logic for subject under test } } But when I run mvn test, I got: ------------------------------------------------------- T E S T S ------------------------------------------------------- Running SimlpeTest Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 Somehow, surefire didn't recognize that test class. My pom.xml looks like: <properties> <java.version>1.8</java.version> <junit.version>5.0.0-SNAPSHOT</junit.version> </properties> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>junit5-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>snapshots-repo</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <updatePolicy>always</updatePolicy> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> Any idea how to make this work?
The maven-surefire-plugin, as of today, does not have full support of JUnit 5. There is an open issue about adding this support in SUREFIRE-1206. As such, you need to use a custom provider. One has already been developed by the JUnit team; from the user guide, you need to add the junit-platform-surefire-provider provider and the TestEngine implementation for the new API: <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <!-- latest version (2.20.1) does not work well with JUnit5 --> <version>2.19.1</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.0.3</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.0.3</version> </dependency> </dependencies> </plugin> </plugins> </build> Also, be sure to declare the junit-jupiter-api dependency with a scope of test: <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.0.3</version> <scope>test</scope> </dependency> </dependencies>
Update 2 Issue has been fixed in Maven Surefire Plugin v2.22.0 New version is available at Maven Central Repository. Maven <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </dependency> Gradle compile group: 'org.apache.maven.plugins', name: 'maven-surefire-plugin', version: '2.22.0' Update As Marian pointed out, the latest version of JUnit 5 Platform Surefire Provider (1.2.0) supports latest version of Maven Surefire Plugin (2.21.0): <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.21.0</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.2.0</version> </dependency> </dependencies> </plugin> Example pom.xml <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.2.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.21.0</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.2.0</version> </dependency> </dependencies> </plugin> </plugins> </build> TestScenario.java package test; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class TestScenario { #Test #DisplayName("Test 2 + 2 = 4") public void test() { Assertions.assertEquals(4, 2 + 2); } } Output (mvn clean install) ... [INFO] --- maven-surefire-plugin:2.21.0:test (default-test) # test --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running test.TestScenario [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in test.TestScenario [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ... Simplest way till today: <plugin> <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.1.0</version> </dependency> </dependencies> </plugin>
From the JUnit 5 documentation : Starting with version 2.22.0, Maven Surefire provides native support for executing tests on the JUnit Platform. Additionally you can 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 just that is enough to make run JUnit 5 tests : <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>davidxxx</groupId> <artifactId>minimal-pom-junit5</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <junit-jupiter.version>5.2.0</junit-jupiter.version> <!--optional below but good practice to specify our java version--> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> <!--optional below --> <!-- add any JUnit extension you need such as --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> </plugins> </build> </project> On my GitHub space I added a working sample maven project that you can browse/clone. URL: https://github.com/ebundy/junit5-minimal-maven-project
I encountered the same problem in August 2019 which I asked about here: Maven silently fails to find JUnit tests to run. These answers led me in the right direction, but I found that you can solve the problem even more concisely. I copied my solution from the JUnit5 sample Maven project. As of JUnit 5.5.1 and maven-surefire-plugin 2.22.2, you do not need to add the junit-platform-surefire-provider dependency. It is enough to have this one dependency and one plugin specified in your pom.xml: <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>
I ran into this issue with JUnit5 and Maven but also noticed that, even if only junit-jupiter-engine was added as a dependency, tests would run on some projects, not on others. And I kind of see the same pattern in the comments here: In #Alex comment above you can see he doesn't have any issue, even with earlier versions of surefire/junit/platform. After scratching my head for some time I realized that those projects where the tests wouldn't run were those where the tests method names dit not contain the word "test". Though this isn't mandated by http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html In other words: just with <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.2.0</version> <scope>test</scope> </dependency> this #Test public void something() { assertTrue(true); } will NOT be run, whereas #Test public void testSomething() { assertTrue(true); } WILL be run ! This issue unfolds as a russian doll... Anyway, +1 for #Mikhail Kholodkov whose updated answer fixes all the issues at once!
Just to complement, surefire 2.22.0 + junit 5.2.0 + platform 1.2.0 also works. Attached is a working pom for your referecne: <?xml version="1.0" encoding="UTF-8"?> <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.jjhome.junit5</groupId> <artifactId>junit5-hone</artifactId> <packaging>jar</packaging> <version>1.0.0-SNAPSHOT</version> <name>junit5-home</name> <properties> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit5.version>5.2.0</junit5.version> <platform.version>1.2.0</platform.version> <surefire.version>2.22.0</surefire.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>${platform.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>${platform.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire.version}</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>${platform.version}</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit5.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
In my case this was because of the TestNG in the classpath (SUREFIRE-1527). Groovy 2.5.5 POM have brought it with the groovy-testng module. Manually specified test-framework provider (as it's described at the https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html) solved the problem: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit-platform</artifactId> <version>2.22.1</version> </dependency>
One thing I noticed that I was able to get it working: Naming my test class ClinicCalendarShould does not get picked up by maven Naming my test class ClinicCalendarTest DOES get picked up by maven So, unless I am missing some sort of configuration or parameter or whatever in the surefire plugin, by default, you need to name your test classes XXXTest.
I was facing the same issue both junit5 and maven-surefire tests were failing. However junit4 was running fine. Below combination worked for me, I don't add the versioning. Use junit-bom for dependency management. Using spring-boot 2.1.4 <dependencyManagement> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.6.1</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> </plugin> </plugins> </build> Make sure to upgrade to the latest version of eclipse
Update 2022 The following now works: <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> And of-course the dependency added: <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.8.2</version> <scope>test</scope> </dependency> The tests are now detected.
There is open issue for surefire 2.20 It is working for me with surfire 2.19 + junit-platform-* 1.0.3
I had a similar problem also causing Surefire to recognize zero tests. My problem turned out to be related to the following (from the JUnit 5.1.0 / maven documentation): Due to a memory leak in Surefire 2.20 and issues running on Java 9, the junit-platform-surefire-provider currently only works with Surefire 2.19.1. I was trying to use the latest versions of Surefire (2.21.0) and junit-platform-surefire-provider (1.1.0), and it did not work (in neither Java 8 or 9) Switching back to Surefire 2.19.1 solved my problem. According to this issue a fix will be included in version 1.2.0 of the junit-platform-surefire-provider (currently available as SNAPSHOT only).
In my case, the surefire plugin didn't get the correct version auf the jupiter-engine/api. And that was even if running Maven 3.6.1 und surefireplugin Version 2.22.2! Now my surefire-plugin configuration looks like: <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> </dependency> </dependencies> </plugin> ... </plugins> </pluginManagement> Further more, I had to force these Versions: <dependencyManagement> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.5.2</version> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-engine</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-commons</artifactId> <version>1.5.2</version> </dependency> </dependencies> </dependencyManagement> Looks like 5.5.2 was linked to the wrong platform version 1.3.2 instead of 1.5.2 in my case. All JUnit5 Tests gets picked up now. Even with 2.22.0 of the surefire plugin this wasn't the case for me! Hope that helps...
Updating to maven-surefire-plugin:2.20 runs the Junit5 tests with no problem. But I am using the M6 version on Junit5.
Be careful with nested tests. I had a test class like this: class AcrTerminalTest { #Nested class card_is_absent { AcrTerminal acrTerminal = new AcrTerminal(new CardAbsentFakeCardTerminal()); #Test void isCardPresent_returns_false() { assertThat(acrTerminal.isCardPresent()) .isFalse(); } } } Running just ./mvnw test -Dtest=AcrTerminalTest failed, I needed to add * after test class name like this ./mvnw test -Dtest=AcrTerminalTest\* (see the asterisk).
This was commented by schnell18 some lines above https://stackoverflow.com/a/51796487/1619489 For me was such easy as: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> </parent> <build> <finalName>app-console-services</finalName> <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> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> </plugin> </plugins> </build> ... And this dependency... <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <scope>test</scope> </dependency>
Based on my case experience there should be no dependency on library junit-jupiter-engine in pom.xml. Then one can use plugin maven-surefire-plugin and dependency on junit-jupiter of the newest version
I ran into a similar problem when upgrading from junit 4 to 5. from <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <type>jar</type> <scope>test</scope> <optional>true</optional> </dependency> to <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.4.0</version> <scope>test</scope> </dependency> I used an Eclipse feature to create the unit test class. Right click the class file to test --> New --> Other --> Junit Test Case --> New JUnit Jupiter test. This will nicely create a stubbed out Unit Test class. Beware however as the stubbed out Unit Test class will NOT have the public class identifier nor will the corresponding annotated test method(s) be public. The unit test will run fine in Eclipse but when run from bash, 'mvn test', the corresponding test will not be detected (no warning either). Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 If you revert to junit 4, 'mvn test' will warn you. Tests in error: initializationError(com.mycompany.operations.ConvertExcelTest): The class com.mycompany.operations.ConvertExcelTest is not public. initializationError(com.mycompany.operations.ConvertExcelTest): Test class should have exactly one public constructor initializationError(com.mycompany.operations.ConvertExcelTest): Method testExecute_excel97File() should be public Tests run: 3, Failures: 0, Errors: 3, Skipped: 0 The solution for me was to make the corresponding test class public and to make the test method public too. public class ConvertExcelTest { #Test public void testExecute_excel97File() {
For me the solution is adding the annotation below on top of the class. #RunWith(JUnitPlatform.class) public class MyTest { .... } Then even the surefire plugin is not required.
Command mvn test seems not find JUnit5 parameterized test
I'm trying to execute my JUnit5 tests on Maven with the command mvn test. I've added maven-surefire-plugin on the pom like this: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M4</version> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> </dependency> </dependencies> </plugin> </plugins> </build> My tests are implemented in four classes, named: IOTest.java OperationTest.java TaskTest.java UtilityTest.java The tests are correctly executed from Eclipse but when I run them from shell with mvn test only the ones annotated with #Test are executed, while those annotated with #ParameterizedTest seem not visible.
i think you are missing the scope, try adding scope in the dependency as shown below <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> <scope>test</scope> </dependency>
cucumber junit runner java.lang.NoSuchMethodError:
Trying to implement cucumber to do some automated tests. jUnit tests. I've created 2 files and edited pom.xml that comes with maven project to add dependencies. Content is shown below. The first of two files are cucumber .feature files, which is a gherkin of plain language. The other is CukesRunner.java When I run my tests using Project -> Run as ... -> Maven test it works as expected. However when I ran the CukesRunner.java file using Eclipse the Eclipse JUnit GUI, I get an error: java.lang.NoSuchMethodError: org.junit.runner.Description.createSuiteDescription(Ljava/lang/String;Ljava/io/Serializable;[Ljava/lang/annotation/Annotation;)Lorg/junit/runner/Description; at cucumber.runtime.junit.FeatureRunner.getDescription(FeatureRunner.java:43) at cucumber.api.junit.Cucumber.describeChild(Cucumber.java:77) at cucumber.api.junit.Cucumber.describeChild(Cucumber.java:41) at org.junit.runners.ParentRunner.getDescription(ParentRunner.java:226) ... 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.bdd</groupId> <artifactId>airportparking</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>airportparking</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.1.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.1.5</version> <scope>test</scope> </dependency> <dependency> <groupId>com.rubiconproject.oss</groupId> <artifactId>jchronic</artifactId> <version>0.2.6</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> </project> CukesRunner.java: package com.bdd.airportparking; import cucumber.api.junit.*; import org.junit.runner.RunWith; #RunWith(Cucumber.class) #Cucumber.Options( format={"pretty", "html:target/cucumber"}, features="src/test/resources" ) public class CukesRunner { } ValetParking.feature: Feature: Valet Parking As a traveler In order to determine where to park my car I want to know the cost of valet parking Scenario: Calculate valet parking cost for half an hour When I park my car in the Valet Parking Lot for 30 minutes Then I will have to pay $12 Output when running CukesRunner.java as a Junit Test: java.lang.NoSuchMethodError: org.junit.runner.Description.createSuiteDescription(Ljava/lang/String;Ljava/io/Serializable;[Ljava/lang/annotation/Annotation;)Lorg/junit/runner/Description; at cucumber.runtime.junit.FeatureRunner.getDescription(FeatureRunner.java:43) at cucumber.api.junit.Cucumber.describeChild(Cucumber.java:77) at cucumber.api.junit.Cucumber.describeChild(Cucumber.java:41) at org.junit.runners.ParentRunner.getDescription(ParentRunner.java:226) at org.junit.runner.Runner.testCount(Runner.java:38) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.countTestCases(JUnit4TestClassReference.java:30) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.countTests(RemoteTestRunner.java:487) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:455) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) How I structured my project in eclipse: http://postimg.org/image/vf6tlw7el/full/
Updating your junit version and maybe also your surefire plugin will fix this problem. <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> For surefire: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> </plugin> </plugins> </build>
I had the same issue and got fixed once I configured the latest version. The issue was on junit version 4.10. the latest version from 4.11 works good <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>
Different story but same issue... developing a Jenkins plugin, using gradle, had the latest junit:junit:4.12 library on my classpath... The issue was being caused by the junit:junit-dep:4.10 library, "aka"... <dependency> <groupId>junit</groupId> <artifactId>junit-dep</artifactId> <version>4.10</version> <scope>test</scope> </dependency> After explicitly removing it from my configuration classpath, I no longer have the issue.
<dependencies> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.2</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.2.2</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.mkolisnyk</groupId> <artifactId>cucumber-reports</artifactId> <version>0.0.11</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <encoding>UTF-8</encoding> <source>1.6</source> <target>1.6</target> <compilerArgument>-Werror</compilerArgument> </configuration> </plugin> </plugins> </build>
I faced the same issue and i found that i have configured both junit 4.10, and 4.11 in my build path, Adhering to junit 11, resolved the issue.
Faced same issue and is resolved by using the JUnit version to 4.11 or higher. Ref: https://groups.google.com/forum/#!topic/cukes/et3rd_0LVRU
Make sure you use the correct JUnit version in your POM.xml. Change it to latest from 4.10 to 4.11 it works.
I have updated my junit driver from 4.9 to 4.11; it is absolutely a junit driver problem, so just update it, and you will get it.
Add my own bundle sources to pax-exam when building with pax-maven-plugin
I'm trying to build my OSGI bundle with pax-maven-build and in the same time test it with pax-exam. It have some bundle in provision than I can test with the following pax-exam test configuration: #RunWith(JUnit4TestRunner.class) #ExamReactorStrategy(AllConfinedStagedReactorFactory.class) public class OSGILoaderTest { #Inject protected BundleContext bundleContext; #Configuration public Option[] config() throws MalformedURLException { String projectRoot = // a path to my project return options( junitBundles(), equinox(), bundle(projectRoot + "libs/org.eclipse.core.variables_3.2.500.v20110511.jar"), bundle(projectRoot + "libs/org.eclipse.core.contenttype_3.4.100.v20110423-0524.jar"), bundle(projectRoot + "libs/org.eclipse.core.expressions_3.4.300.v20110228.jar"), // etc... ); } #Test public void getBundleContext() throws RodinDBException { IRodinDB rodinDB = RodinCore.getRodinDB(); assertNotNull(rodinDB); } } Here, I can see I can access to the IRodinDB instance from a jar I have provisonned. Now I have code my own bundle, which is going to use all the jar provisionned. But I cannot even test my own code, for instance: #Test public void checkAccessToRodinDbTest() { VTGService service = null; assertTrue(true); } give an error at compilation time: [ERROR] Failed to execute goal org.ops4j:maven-pax-plugin:1.5:testCompile (default-testCompile) : Compilation failure [ERROR] cannot find symbol [ERROR] symbol : class VTGService It seems test compilation cannot see 'src/main/java', contrarly as expected by the default behavior of maven-compiler-plugin. But in my case, you can see than maven does not use the compiler plugin but instead maven-pax-plugin. The question is: how can i test my own bundle with pax-exam ? update1 It seems that this is a problem with recent version of maven-pax-plugin, as the basic example available in ops4j pax maven plugin (in section Using the Pax Plugin inside a POM) seems to suffer of the same problem. update2 As requested by Dmytro, this is the pom.xml of my bundle: <?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <relativePath>../poms/compiled/</relativePath> <groupId>fr.xlim.ssd.vtg.build</groupId> <artifactId>compiled-bundle-settings</artifactId> <version>0.1-SNAPSHOT</version> </parent> <properties> <bundle.symbolicName>fr.xlim.ssd.vtg.bundle</bundle.symbolicName> <bundle.namespace>fr.xlim.ssd.vtg.bundle</bundle.namespace> </properties> <modelVersion>4.0.0</modelVersion> <groupId>fr.xlim.ssd.vtg</groupId> <artifactId>fr.xlim.ssd.vtg.bundle</artifactId> <version>0.1-SNAPSHOT</version> <name>${bundle.symbolicName}</name> <packaging>bundle</packaging> <dependencies> <dependency> <type>pom</type> <groupId>${project.parent.groupId}</groupId> <artifactId>provision</artifactId> <optional>true</optional> </dependency> <!-- not needed as equinox bundle are available in provision --> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi_R4_core</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi_R4_compendium</artifactId> <optional>true</optional> </dependency--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam</artifactId> <version>2.3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam-junit4</artifactId> <version>2.3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam-inject</artifactId> <version>2.3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.ops4j.pax.url</groupId> <artifactId>pax-url-mvn</artifactId> <version>1.3.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam-container-native</artifactId> <version>2.3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.ops4j.pax.exam</groupId> <artifactId>pax-exam-link-mvn</artifactId> <version>2.3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project> I am not sure it is the most elegant solution, but I created a new maven project when I can import my own bundle like in the source code of my question. Is there an elegant way to add my own java sources directly as new bundle for test in the same Maven project ? It could be not possible (as the bundle assembly operation is done after the compilation and tests)...
I use the following setup to provision the bundle under test. When configuring the test, I provision the bundle using the reference-protocol (this is a non-standard feature of Equinox and Felix, see here): #Configuration public Option[] config() { return options( bundle("reference:file:target/classes"), junitBundles(), felix() ); } The test-cases also run when you specify knopplerfish() as the environment. I guess that is because the URL is resolved by Pax Exam, and not by the OSGi-runtime. I use the maven-bundle-plugin to build my bundles. To make this work as expected, you have to add the following configuration: <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.3.7</version> <extensions>true</extensions> <executions> <!-- This execution makes sure that the manifest is available when the tests are executed --> <execution> <goals> <goal>manifest</goal> </goals> </execution> </executions> </plugin> </plugins> </build> Otherwise the manifest will not be available when the tests are run, since by default it is generated during the package-phase. I hope I did not forget anything - please let me know if it worked for you!
Check PaxExam docs how to configure your Maven POM with PaxExam. Samples here