25

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?

Ava
  • 502
  • 1
  • 6
  • 21
  • 1
    which version of junit you are trying to use? From you test class, it appears to me you want to use junit 5 but from your maven configuration seems for junit 4. – skadya Sep 22 '17 at 09:04
  • 1
    I can't see 'package' line in your test class? Is your test class in default package? – P.An Sep 22 '17 at 09:04
  • 1
    I tried to recreate your example. I get this error: `initializationError(package.maven.MyTest): The class package.maven.MyTest is not public.` Can you try adding public modifier to your class? – SilverNak Sep 22 '17 at 09:06
  • @SilverNak that did it for me! No errors saying as much, but you mentioned that his class wasn't public. I looked, and none of my classes were public either. And Eclipse created those classes for me! How dare they! :) Thanks. – Marvo Nov 07 '22 at 05:20

7 Answers7

27

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>
...
Community
  • 1
  • 1
glytching
  • 44,936
  • 9
  • 114
  • 120
  • 3
    From the above docs link, this is no longer recommended: "The junit-platform-surefire-provider, which was originally developed by the JUnit team, was deprecated in JUnit Platform 1.3 and discontinued in 1.4. Please use Maven Surefire’s native support instead." – Harry King Nov 27 '20 at 15:32
17

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

RudyD
  • 667
  • 7
  • 11
7

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

Amazia Gur
  • 1,692
  • 17
  • 16
4

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>
David DeMar
  • 2,390
  • 2
  • 32
  • 45
0

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>
Dharman
  • 30,962
  • 25
  • 85
  • 135
AndaluZ
  • 1,410
  • 3
  • 15
  • 33
  • 1
    This has changed between JUnit 4 and Junit 5: test classes, test methods, and lifecycle methods are no longer required to be public, but they must not be private. So you can just omit the 'public' modifier, so they use the default visibility (package local). – GeertPt Oct 25 '21 at 10:18
  • 1
    No it's not. If you do not specify either public or private, it's considered package-local, and JUnit 5 will still find your tests. See e.g: https://github.com/junit-team/junit5-samples/blob/r5.5.1/junit5-jupiter-starter-maven/src/test/java/com/example/project/CalculatorTests.java – GeertPt Oct 25 '21 at 14:39
0

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

faro
  • 11
  • 2
0

for me:

maven-surefire-plugin 2.19 -> tests not running

maven-surefire-plugin 2.22.2 -> tests not running

maven-surefire-plugin 3.1.0 -> OK

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>   
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.0</version>
</plugin>