Main Tutorials

JUnit 5 – How to disable tests?

JUnit 5 disabled

JUnit 5 @Disabled example to disable tests on the entire test class or individual test methods.

P.S Tested with JUnit 5.5.2

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.

output

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

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Emil Cazacu
3 years ago

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

Karim Ourrai
4 years ago

What’s the difference between @Disabled and @Ignore I mean when I should use one and not the other.

Michael
4 years ago
Reply to  Karim Ourrai

@Ignore is JUnit 4, @Disabled ist JUnit Jupiter