简体   繁体   中英

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

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.

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>

@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

Maybe check if Jupiter conditions ( @Disabled in your case) are deactivated via configuration parameter junit.jupiter.conditions.deactivate .

Please see JUnit5 documentation for more details: https://junit.org/junit5/docs/current/user-guide/#extensions-conditions-deactivation

this might be problem with improper imports. i solved it by removing all junit import statements and re-imported all junit classes properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM