Disabling JUnit Tests
We might want to disable or skip some test cases during the testing process because either they are already tested before or there is an issue in the functionality. We can disable the tests by commenting or removing the test annotations before the method or class. But the reports of the disabled test cases will not be generated by doing this. So we use an alternate technique, i.e., using @Disabled annotation before the class or method.
@Disabled annotation disables the tests and also generates the corresponding reports. It takes a string as an optional parameter stating the reason why the test is disabled. Let’s explore how to use this annotation with both the classes and methods.
Disabling Test Classes
@Disabled("Disable until issue gets fixed")
public class Junit5DisableClassTest {
@Test
void testOnDev() {
assertEquals(5, MathUtil.add(3, 2));
}
@Test
void testOnProd() {
assertEquals(15, MathUtil.multiple(3, 5));
}
}

You can also try this code with Online Java Compiler
Run Code

Source
In the above, we added @Disabled annotation to the Junit5DisableClassTest, which has methods testOnDev and testOnProd that contain the test codes. The test codes inside methods are not run, but the tests ate generated.
Disabling Test Methods
public class Junit5DisableClassTest {
@Test
void testOnDev() {
assertEquals(5, MathUtil.add(3, 2));
}
@Disabled("Disable until issue gets fixed")
@Test
void testOnProd() {
assertEquals(15, MathUtil.multiple(3, 5));
}
}

You can also try this code with Online Java Compiler
Run Code

Source
In the above code, the class Junit5DisableClassTest has two test methods testOnDev and testOnProd, which contains the test codes. The @Disabled annotation is mentioned before the testOnProd method to prevent it from executing the tests. But the test reports are still generated.
FAQs
-
What are JUnit tests?
A test in JUnit is a method or class used only for testing. These are called Test methods or Tests classes. The tests contain the code used for testing the behavior of the application.
-
Why do we disable tests?
We disable the tests in some cases to disable them from running because of reasons like; either they are already tested before or there is an issue in the functionality. So we don’t want to include these tests, which might make it hard to identify the errors.
-
How can I disable tests?
You can disable the test classes or methods by writing an annotation @Disabled before the test class or method. It prevents the test from running but generates the reports to keep track of them.
-
Why do we define an optional parameter in @Disabled annotation?
We provide a string as the optional parameter inside the Disabled annotation to specify the reason for disabling the test case, so the other testers and developers can understand why we disabled the test case.
-
What does @Test annotation do?
The @Test annotation indicates that the method or class is a test block, and the code inside it is run to test the code.
Key Takeaways
We discussed JUnit tests and how to represent a test method in JUnit. Then we learned how to disable the tests and use @Disabled annotations for disabling them along with their outputs.
Hey Ninjas! Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.
Happy Learning!