Newly-Introduced JUnit 5 Annotations and Classes. The @Disabled annotation

Newly-Introduced JUnit 5 Annotations and Classes. The @Disabled annotation

The second part of our article on Newly-Introduced JUnit 5 Annotations and Classes. This time we look at @Disabled annotation.

2. The @Disabled annotation


The @Disabled annotation can be used over classes and test methods. It’s used to signal that the annotated test class or test method is currently disabled and shouldn’t be executed. We should give reasons for disabling a test, allowing the rest of your team to know exactly why this has been done. If it’s applied on a class, it disables all the methods of the test. The disabled tests are also shown differently when each programmer is running them from the IDE, and the disabling reason is displayed into their console.

The usage of the annotation is demonstrated by the classes disabled.DisabledClassTest and disabled.DisabledMethodsTest. Listings 2 and 3 show the code for these classes.

usage of the @Disabled annotation on a test class.JPG


The whole testing class is disabled, and a reason is provided (1). This is the recommended way to work, providing your colleagues (or maybe, at a later time, even you, the author of the class) immediate understanding about why the test isn’t enabled right now.

usage of the @Disabled annotation on methods.JPG


You see that:
  1. The code provides two tests, both of them disabled.
  2. One of the tests is disabled without a given reason (1).
  3. The other test is disabled with a reason that may be quickly understood (2) – the recommended way to work.

Interested in JUnit? Check out our trainings.


Catalin Tudose
Java and Web Technologies Expert
Nadal masz pytania?
Połącz sięz nami