JUnit 5 Disabling Tests Examples

1. Overview

We use @Ignore annotation is used to disable or ignore the tests in JUnit 4. JUnit 5 provides a @Disabled annotation to disable test class or test methods.

2. JUnit Disabling Tests Example

Entire test classes or individual test methods may be disabled via the @Disabled annotation, via one of the annotations discussed in Conditional Test Execution, or via a custom ExecutionCondition.

2.1 Disable Test Class

Use @Disabled annotation to disable entire test class.
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@Disabled
class DisabledClassDemo {
    @Test
    void testWillBeSkipped() {
    }
}

2.1 Disable Test Method

Use @Disabled annotation to disable entire test method.
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class DisabledTestsDemo {

    @Disabled
    @Test
    void testWillBeSkipped() {
    }

    @Test
    void testWillBeExecuted() {
    }
}

2.3 More Examples

package com.javadevelopersguide.junit5;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@Disabled
class DisabledClassDemo {
    @Test
    void testWillBeSkipped() {
     System.out.println("testWillBeSkipped");
    }
    
    @Test
    void testWillBeExecuted() {
     System.out.println("testWillBeExecuted");
    }
    
    @Test
    void test1() {
     System.out.println("test 1");
    }
    
    @Test
    void test2() {
     System.out.println("test 2");
    }
    
    @Test
    void test3() {
     System.out.println("test 2");
    }
}

3. Conclusion

Comments