Contents

JUnit disable test using JUnit 5 or 4

Written by: David Vlijmincx

Introduction

This post explains how to disable or ignore a unit test and an entire test class. The annotations differ between JUnit 5 and JUnit 4, so I will cover both annotations in this post.

Disable (skip) unit tests in JUnit 5 /

To disable a test or test class in JUnit 5 you use the @Disabled annotation. In the following example, I use the @Disabled annotation to disable a single unit test in JUnit 5.

1
2
3
4
5
@Disabled
@Test
void testOne() {
    // your testing code
}

The example shows how you can ignore a test with JUnit 5.

Disable a test class

To ignore all the tests inside a class, you place the @Disabled annotation at the class level. In the following example we placed the annotation above the class name. Doing so will disable all the unit tests inside the class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@Disabled
public class YourTestClass {

    // All unit tests inside this class are disabled
    @Test
    void testOne() {
        // your testing code
    }

}

Ignore unit tests in JUnit 4

To ignore a unit tests in JUnit 4 you use the @Ignore annotation. In the following example, I use the @Ignore annotation to disable a single unit test in JUnit 4.

1
2
3
4
5
@Ignore
@Test
void testOne() {
    // your testing code
}

Ignore a test class

To ignore all the unit tests inside a class, you have to place the @Ignore annotation at the class level. In the following example the @Ignore is above the class, so none of the unit tests inside the class will be executed.

1
2
3
4
5
6
7
8
9
@Ignore
public class YourTestClass {

    @Test
    void testOne() {
        // your testing code
    }

}

Conclusion

In this post, we looked at how to disable and ignore unit tests using JUnit 5 and JUnit 4. You learned how skip a test with JUnit using both JUnit 5 and 4. Using @Ignore or @Disabled will make Junit skip the test.

Further reading

More about testing in Java: