@Disabled is not working in JUnit5

你说的曾经没有我的故事 提交于 2019-12-14 00:52:45

问题


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?


回答1:


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>



回答2:


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.




回答3:


@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



来源:https://stackoverflow.com/questions/52054577/disabled-is-not-working-in-junit5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!