Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 734
Carmageddon Avatar asked Nov 06 '18 20:11

Carmageddon


People also ask

Why tests are not running in Maven?

Test Method Not public Also, the test method could have been marked as private by mistake. Until JUnit 4, Maven will only run the test classes which are marked as public. We should note, though, that this won't be an issue with JUnit 5+.


4 Answers

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

like image 152
Luís Soares Avatar answered Oct 18 '22 09:10

Luís Soares


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!

like image 40
Carmageddon Avatar answered Oct 18 '22 08:10

Carmageddon


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>
like image 6
garg10may Avatar answered Oct 18 '22 08:10

garg10may


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

like image 5
Michael Legart Avatar answered Oct 18 '22 08:10

Michael Legart