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

author image
mkyong
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

avatar
1 Comment threads
0 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Karim Ourrai Recent comment authors
newest oldest most voted
Karim Ourrai
Guest
Karim Ourrai

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