
JUnit 5 @Disabled
示例禁用整个测试类或单个测试方法上的测试。
PS已通过JUnit 5.5.2测试
注意
您还可以根据条件禁用测试 。
1. @禁用方法
1.1测试方法testCustomerServiceGet
被禁用。
DisabledMethodTest.java
package com.mkyong.disable;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DisabledMethodTest {
@Disabled("Disabled until CustomerService is up!")
@Test
void testCustomerServiceGet() {
assertEquals(2, 1 + 1);
}
@Test
void test3Plus3() {
assertEquals(6, 3 + 3);
}
}
输出–使用Intellij IDE运行。

2. @在课堂上禁用
2.1整个测试课程将被禁用。
DisabledClassTest.java
package com.mkyong.disable;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@Disabled("Disabled until bug #2019 has been fixed!")
public class DisabledClassTest {
@Test
void test1Plus1() {
assertEquals(2, 1 + 1);
}
@Test
void test2Plus2() {
assertEquals(4, 2 + 2);
}
}
经过测试,它可以在Maven或Gradle构建工具中按预期工作。
注意
但是,在Intellij IDE下运行上述测试,类级别的@Disabled
无法按预期工作,不知道为什么吗?
下载源代码
$ git clone https://github.com/mkyong/junit-examples
$ cd junit5-examples
$检查src / test / java / com / mkyong / disable / *。java
参考文献
翻译自: https://mkyong.com/junit5/junit-5-how-to-disable-tests/