【问题标题】:@Disabled is not working in JUnit5@Disabled 在 JUnit5 中不起作用
【发布时间】:2019-02-02 21:25:51
【问题描述】:

我想在 Maven 项目中使用 Junit 5:

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

我现在想禁用测试:

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 {
    .... 
}

但它不起作用。测试在 mvn clean build 之后执行。你能告诉我缺少什么吗?

【问题讨论】:

  • 您能否判断结果是否显示没有跳过任何测试?它应该可以按需要工作,但可能看起来禁用的测试已执行。当我运行一个包含一个禁用的 n 测试用例的测试类时,结果看起来像 Runs: n/n (1 skipped)
  • 用 maven 3.5.3 和 junit-jupiter-engine 5.0.1 测试,工作正常,跳过测试。
  • @staszko032 你能粘贴你的 POM 配置吗?可能我错过了什么。
  • 在此之前,您能否检查一下您是否在 POM 中配置了 maven-surefire-plugin?
  • 是的 - 我在 POM 中缺少 maven-surefire-plugin。谢谢!

标签: java eclipse junit5


【解决方案1】:

这是由maven-surefire-plugin 和junit5 之间的不兼容引起的。您要么必须为 maven-surefire-plugin 定义至少具有 2.22.0 的版本(请参阅 codefx blog - junit 5 setup),要么只使用 maven 3.6.0。 此外,您必须具有对 jupiter-engine 的依赖关系,如上述问题的第一行所述:

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

如果您仅定义了对工件 junit-jupiter-api 的依赖项,这足以让测试编译并使用 junit5 运行,@Disabled 注释将被静默忽略,并且特定测试也将运行。

【讨论】:

    【解决方案2】:

    检查你的surefire插件配置是否依赖于junit-jupiter-engine。我不确定,但我认为应该配置它以便从引擎工件加载所有功能,包括禁用注释。

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

    【讨论】:

    【解决方案3】:

    @staszko032 你描述的POM中的xml代码是2018年的,2019年我们在Apache项目中采用了JUnit5 provider,代码https://stackoverflow.com/a/52056043/2758738已经不合法了。

    @Jens Vagts JUnit5 和 Surefire/Failsafe 之间没有不兼容,因为 Apache 插件使用 JUnit5 平台启动器来完成所有这些技巧。Surefire 和 Failsafe 插件不会执行您的测试。所以没有什么不兼容的,因为所有的工作都是由 JUnit5 引擎完成的,而不是插件的代码。它是执行测试的 POM 中定义的真正引擎。该插件仅触发完成所有工作并将事件发送回插件的引擎。

    请参阅 Apache 文档:

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

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

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多