@Disabled is not working in JUnit5

2019-07-15 05:34发布

问题:

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.