Surefire is not picking up Junit 5 tests
Asked Answered
A

21

159

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?

Ashlar answered 1/5, 2016 at 18:32 Comment(6)
Right now you have to use a special implementation of the surefire-plugin. Check the examples of the junit team hereCarcanet
The question is based on an outdated version but this answer is based on the current one (as of December 2016).Alvita
@Nicolai Thanks for updating the answer. Anyway I would appreciate your edit on question, if you have the time.Ashlar
This error does not occur like that anymore. The most likely case for a lack of test execution is covered by this question.Alvita
Make sure the test files are in the right place. By default test should be a sibling of main under the src folder.Breathed
Please see this answer below for current versions (2.22.x) of the surefire-plugin. Adding <artifactId>junit-jupiter-engine</artifactId> oder <artifactId>junit-jupiter</artifactId> as a dependency (not in the plugin) may help.Lattermost
R
138

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>
Rom answered 1/5, 2016 at 18:42 Comment(9)
Worth to note that at this moment surefire 2.20.1 doesn't work with junit 5 provider thus 2.19.1 used in sample is still valid despite of passed year.Nameless
surefire 2.21.0 doesn't work too, have to rollback to 2.19.1Bladdernose
@Bladdernose For me it does work with 2.21.0 and newer versions of the plugin and junit5. See junit.org/junit5/docs/current/user-guide/…Malinowski
Verified: maven-surefire-plugin v2.21.0 works fine with junit-jupiter-engine v5.2.0 and junit-platform-surefire-provider v1.2.0Anyplace
This works for me!. Thanks!. I am using spring-boot 2.0.4.RELEASE. Also with maven-surefire-plugin 2.21.0 did not work.Apache
November 2018: still have to use junit-platform-surefire-provider, at version 1.3.1, even though the Junit documentation claims otherwise.Ecthyma
This config will fail with Surefire 2.22.0 or higher. You need to exclude the Junit deps from the Surefire config. I wrote a quick post about it here - springframework.guru/…Slaveholder
The junit-platform-surefire-provider has been deprecated and is scheduled to be removed in JUnit Platform 1.4. Please use the built-in support in Maven Surefire >= 2.22.0 instead. junit.org/junit5/docs/current/user-guide/…Gallstone
> be sure to declare the junit-jupiter-api dependency with a scope of test:::: Thanks! This was the clue when Maven stopped detecting my JUnit 5 tests after upgrading junit-jupiter from 5.8.2 to 5.9.0.F
G
79

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>
Grower answered 27/2, 2018 at 23:9 Comment(3)
This only works because of Test in the name of the class. Rename the class to ExampleScenario - it will not be discovered. (Surefire 2.22.0)Nealah
@AlexeiVinogradov Yes. It's expected behavior. More information available here: https://mcmap.net/q/73457/-maven-does-not-find-junit-tests-to-runGrower
bevare, junit engine version vs. spring boot parent version. Just specify dependency junit-jupiter-engine:5.1 does not work where newer versions does. Spring boot has still preconfigured JUnit4 <junit.version>4.13.Raving
B
25

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

Bentonbentonite answered 14/8, 2018 at 9:35 Comment(0)
U
20

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>
Until answered 15/8, 2019 at 12:8 Comment(2)
This is the correct and concise approach. Works with OpenJDK 11 and JUnit 5.6.2.Somniloquy
perfect for me, the other options have not workedErvin
B
9

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!

Backbreaking answered 25/5, 2018 at 10:6 Comment(4)
I have also just experienced an issue with mvn 3.5.0, jdk 1.8.0_101 where my class name did not have 'Test' in and the test was not picked up, this helped me find that!Toreutics
Yeah indeed you should be compliant to maven.apache.org/surefire/maven-surefire-plugin/examples/… But I was talking about something different.Backbreaking
Yes it turned out I had two problems, the first was not remembering the basic maven surefire test rules. The second was not doing a Maven > update project for eclipse to pick up the junit 5 launcher.Toreutics
I would like to add that the class and methods must be public.Sac
T
6

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>
Towney answered 11/8, 2018 at 4:33 Comment(2)
My issue was that I did not have junit-vintage-engine as a dependency, and all of my tests were written for JUnit 4.Northerly
the combination of specified junit, platform and surefire versions worked for me. thanks !Rahr
A
6

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>
Ashantiashbaugh answered 21/3, 2019 at 14:22 Comment(2)
You should wrap the <dependency> in <dependencies> in your code sample.Softspoken
you saved me thanks...groovy-all included groovy-testng and after excluding it....tests are runningGore
T
4

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.

Trimerous answered 7/1, 2022 at 14:54 Comment(1)
It seems we are back to this problem again with 3.0.0-x.Slowwitted
G
3

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.

Gonadotropin answered 30/12, 2018 at 6:37 Comment(0)
D
3

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

Divest answered 29/3, 2020 at 16:53 Comment(0)
M
1

There is open issue for surefire 2.20

It is working for me with surfire 2.19 + junit-platform-* 1.0.3

Mcmahan answered 24/1, 2018 at 23:2 Comment(0)
A
1

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).

Autotomy answered 26/3, 2018 at 11:46 Comment(1)
I've tried the SNAPSHOTs and all is working (with Surefire 2.21.0). Hopefully it be still working at the time of release.Submiss
C
1

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...

Corkboard answered 23/10, 2019 at 9:54 Comment(0)
X
0

Updating to maven-surefire-plugin:2.20 runs the Junit5 tests with no problem.

But I am using the M6 version on Junit5.

Xenophobe answered 4/8, 2017 at 8:31 Comment(3)
Not that simple (at least at the moment). See JUnit 5 User GuideSarette
@Sarette weird its works just fine on mine with the m6 version.Xenophobe
I tired surefire-2.20.1 + junit-5.0.2 and it didn't work. I also tried surefire-2.20.1 + junit-5.1.0-M6 and it didn't workNissensohn
S
0

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).

Stretch answered 1/6, 2021 at 18:53 Comment(0)
P
0

This was commented by schnell18 some lines above https://mcmap.net/q/74608/-surefire-is-not-picking-up-junit-5-tests

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>
Pullen answered 30/11, 2021 at 19:31 Comment(0)
E
0

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

Evalyn answered 25/1, 2022 at 1:1 Comment(0)
M
0

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() {
Minimus answered 15/6, 2022 at 13:9 Comment(0)
B
0

I had a similar issue, which is why I ended up here. For me, I had jupiter on the classpath and surefire was at 3.2.2 and was running with the @SpringBootTest annotation so most of the advice here was/is out of date.

The key thing for me from running the tests was:

...
[INFO] --- surefire:3.2.2:test (default-test) @ my-tests ---
[INFO] Using auto detected provider org.apache.maven.surefire.junit.JUnit3Provider
...

The surefire website provides instructions on how to select a provider.

Essentially, for me this fixed it against junit5:

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.2.2</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit-platform</artifactId>
                        <version>3.2.2</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <argLine>-Dspring.profiles.active=${spring.profiles.active} ${args}</argLine>
                </configuration>
            </plugin>

At the time of posting surefire have these providers:

The providers supplied with Surefire are surefire-junit3, surefire-junit4, surefire-junit47, surefire-junit-platform and surefire-testng.

Braunite answered 21/11, 2023 at 12:14 Comment(0)
H
0

You need check your JUnit version.

If nothing is configured, Surefire detects which JUnit version to use by the following algorithm:

if the JUnit 5 Platform Engine is present in the project
    use junit-platform
if the JUnit version in the project >= 4.7 and the <<<parallel>>> configuration parameter has ANY value
    use junit47 provider
if JUnit >= 4.0 is present
    use junit4 provider
else
    use junit3.8.1

You can also force a specific provider by adding it as a dependency to Surefire itself.

For example:


<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.22.2</version>
        </dependency>
    </dependencies>
</plugin>
Hadlee answered 20/1 at 4:52 Comment(0)
J
-1

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.

Justifiable answered 9/12, 2020 at 5:21 Comment(1)
@RunWith is from JUnit 4, so I don't think that applies to this question about JUnit 5 tests. (You should add junit-vintage-engine as a dependency to maven-surefire-plugin if you want to run JUnit 4 tests under JUnit 5.)Deviation

© 2022 - 2024 — McMap. All rights reserved.