JUnit 5 – How to disable tests?
JUnit 5 @Disabled
example to disable tests on the entire test class or individual test methods.
P.S Tested with JUnit 5.5.2
Note
You can also disable tests based on conditions.
You can also disable tests based on conditions.
1. @Disabled on Method
1.1 The test method testCustomerServiceGet
is disabled.
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);
}
}
Output – Run with Intellij IDE.
2. @Disabled on Class
2.1 The entire test class will be disabled.
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);
}
}
Tested, it is working as expected in Maven or Gradle build tool.
Note
However, run the above test under Intellij IDE, the
However, run the above test under Intellij IDE, the
@Disabled
on class level is NOT working as expected, no idea why?
Download Source Code
$ git clone https://github.com/mkyong/junit-examples
$ cd junit5-examples
$ check src/test/java/com/mkyong/disable/*.java
$ cd junit5-examples
$ check src/test/java/com/mkyong/disable/*.java
@Disabled works weird in Intellij because it is a feature!
https://youtrack.jetbrains.com/issue/IDEA-194561
If
– in Intellij Idea
– and a test class is marked as @Disabled
– and manually start the execution
Then
you acknowledged the fact it is disabled and yet you want to run it, so your decision is clear, overrule the annotation and execute the test.
Very puzzling.
What’s the difference between @Disabled and @Ignore I mean when I should use one and not the other.
@Ignore is JUnit 4, @Disabled ist JUnit Jupiter