Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Disabled is not working in JUnit5

I want to use Junit 5 in Maven project:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.2.0</version>
    <scope>test</scope>
</dependency>

I want currently to disable the test:

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;

@Disabled
public class DatabaseFeaturesBitStringTest {
    .... 
}

But it's not working. Tests are executed after mvn clean build. Can you advise what I'm missing?

like image 816
Peter Penzov Avatar asked Aug 28 '18 09:08

Peter Penzov


3 Answers

This is caused by incompatibilities betweeen maven-surefire-plugin and junit5. Your either have to define a version with at least 2.22.0 for the maven-surefire-plugin (see codefx blog - junit 5 setup) or just use maven 3.6.0. Additionally you have to have the dependency to the jupiter-engine defined as already stated in the very first lines of this question above:

<dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-engine</artifactId>
   <version>5.4.0</version>
   <scope>test</scope>
</dependency>

If you defined a dependency to the artifact junit-jupiter-api only, which is sufficient to get the test compiled and run with junit5, the @Disabled annotation will be silently ignored and the particular test will get run as well.

like image 50
Jens Vagts Avatar answered Sep 19 '22 14:09

Jens Vagts


Check your configuration of surefire plugin for junit-jupiter-engine dependency. I am not sure, but I believe it should be configured in order to load all features from engine artifact, including Disabled annotation.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>${junit.platform.surefire.provider.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.version}</version>
                </dependency>
            </dependencies>
        </plugin>
like image 20
staszko032 Avatar answered Sep 19 '22 14:09

staszko032


@staszko032 The xml code in POM you described is the era of 2018. In 2019 we adopted JUnit5 provider to Apache project and the code https://stackoverflow.com/a/52056043/2758738 is no more legal.

@Jens Vagts There are no incompatibilities between JUnit5 and Surefire/Failsafe because Apache plugin uses JUnit5 platform launcher which does all this trick.Surefire and Failsafe plugin does not execute your tests. So there is nothing to be incompatible, since all the work is done by JUnit5 engines and not plugin's code. It is the real engine defined in your POM which executes the tests. The plugin only triggers the engine which does all the work and sends the events back to plugin.

Please see Apache documentation:

https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

https://maven.apache.org/surefire/maven-failsafe-plugin/examples/junit-platform.html

like image 38
tibor17 Avatar answered Sep 18 '22 14:09

tibor17