Carmageddon
Carmageddon

Reputation: 2849

No tests were found - Empty test suite when running jUnit 5 testcase on bare-bone Spring Boot Maven project

so we got the message

Process finished with exit code 0
Empty test suite.

When trying to run Junit5 testscases on our multi module project, so for testing the sources of the issue, I decided to try a new project from scratch (I am using IntelliJ IDEA 2018.2.4).

I am getting the same issue with the following simple project POM file:

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.org.bu.dep.app</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.0.5.RELEASE</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>com.vaadin.external.google</groupId>
                    <artifactId>android-json</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I tried with/without the junit-vintage-engine same results.

Follows is the test class:

package com.org.bu.dep.app.demo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
        Assertions.assertTrue(true);
    }

}

On the Run window, it says No tests were found.

I tried many variations such as with/without @SpringBootTest annotation, I tried @ExtendWith(SpringExtension.class) as well to no avail.

I already googled and most results/SackOverflow answers seem to be from 2016/2017 focused on older versions of IntelliJ which had issues with Junit 5, which do not seem to be related.

Please, any advice? If we spend too much time on it will be forced to revert to 4, and I very much do not wish to do that :)

Upvotes: 23

Views: 49863

Answers (11)

Gico
Gico

Reputation: 1394

ok, that was a nightmare for me. finally resolved it.

originally i had a dependency to:

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite-api</artifactId>
    <scope>test</scope>
</dependency>

then i noticed in the docs: https://junit.org/junit5/docs/5.11.1/api/org.junit.platform.suite.api/org/junit/platform/suite/api/package-summary.html

Test suites can be run on the JUnit Platform in a JUnit 5 environment via @Suite with the junit-platform-suite-engine.

and i replaced the above dependency with

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite-engine</artifactId>
    <scope>test</scope>
</dependency>

Upvotes: 2

AdrienW
AdrienW

Reputation: 3462

Adding mine to the list of "dumb" mistakes: I imported the wrong @Test annotation. Junit5 will only consider methods annotated with org.junit.jupiter.api.Test and not org.junit.Test.

Upvotes: 18

Andrew Cheong
Andrew Cheong

Reputation: 30293

If you write your tests in interfaces or abstract classes, e.g.

public interface FooTests() {

  @Test
  default void fooTest() {
    ...
  }
}

which then get implemented by a "real" tester class,

public class Tester() implements FooTests, BarTests {
  ...
}

then what might be happening is that you ran one of those tests for the first time by itself, causing IntelliJ to remember its run configuration thereafter as FooTests::fooTest when really it needs to be Tester::fooTest. You can override this by modifying the test's run configuration, e.g.

enter image description here

You want to replace the Method: class with your non-interface, non-abstract test runner class.

Upvotes: 0

Robse
Robse

Reputation: 915

I know this is an old question but maybe it helps someone. I got the error message because I copy-pasted a method from a scratch file where the method was declared static.

Upvotes: 0

Pavan Ulichi
Pavan Ulichi

Reputation: 79

Using IntelliJ, I had to delete the target folder and run tests to get rid of this error

Upvotes: 0

Lu&#237;s Soares
Lu&#237;s Soares

Reputation: 6222

In my case, I had a private test method - it should be public.

Upvotes: 30

ozinal
ozinal

Reputation: 37

Go to: File -> Project Settings -> Modules and add the below dependency.

add dependency

add 'com.github.database-rider:rider-junit5:1.2.5'

enter image description here

'com.github.database-rider:rider-junit5:1.2.5' will make tests find and run.

Upvotes: 1

garg10may
garg10may

Reputation: 6180

What worked for me.

Don't use the version yourself add junit-bom

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

Also with maven-surefire-plugin don't add the version

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Upvotes: 7

Carmageddon
Carmageddon

Reputation: 2849

So, what works for Junit 5 with Spring Boot 2 is the following 3 TOGETHER:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>

Tested and verified with empty project.

Please note that @Michael Legart's answer suggesting to add

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.1</version>
</plugin>

Is needed for maven test (and Jenkins) to detect and execute the tests!

Upvotes: 21

Michael Legart
Michael Legart

Reputation: 820

I'm pretty sure this is because Spring Boot 2 is not using the required Maven Surefire plugin version for JUnit 5. Try with:

   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.22.1</version>
   </plugin>

JUnit5 support was added in 2.22.0, see https://issues.apache.org/jira/browse/SUREFIRE-1330

Upvotes: 5

DaviM
DaviM

Reputation: 324

Since you're using Intellij. Did you mark your java test folder as Test Sources Root?

Example:

enter image description here

Upvotes: 1

Related Questions