How to Ignore Test Cases in JUnit

Published Jul 20, 2021

Today, I was looking for a way to ignore some JUnit test cases.

Let’s take a look at the JUnit docs.

@Disabled is used to signal that the annotated test class or test method is currently disabled and should not be executed. When applied at the class level, all test methods within that class are automatically disabled as well.

So, there appears to be a @Disabled annotation we can use to ignore tests in Java.

Ignore a JUnit Test Method

To ignore a single test method, we can apply this annotation directly above the method declaration.

We can also include an optional parameter to describe the reason for disabling that test.

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

public class TestClass {
    @Disabled("Test not ready yet")
    @Test
    void testMethod() {
      // ...some test case
    }
}

Ignore a JUnit Test Class

To ignore all test methods within a class, we can apply this annotation directly above the class declaration.

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

@Disabled
public class TestClass {
    @Test
    void testMethod1() {
      // ...some test case
    }
    @Test
    void testMethod2() {
      // ...some test case
    }
}

More Java Articles