How to Disable JUnit Test Method

In this lesson, you will learn about annotation that will help you to disable the test.

Usually, it is not a good idea to disable a test. If the test is failing for some reason and you are not sure why it is failing, do not disable it. Try to figure out why it is failing and fix it. But if you do need to disable the test, there are couple of ways to do it.

@Disabled Annotation

The best way to disable a test method is to use the @Disabled annotation. @Disabled annotation is used to tell JUnit 5 engine that the annotated test class or test method is currently disabled and should not be executed. Test method will not be executed and will be included in the test report as a disabled test.

Notice how in the below code example, the method is annotated with a @Disabled annotation. You can also use it above the class and disable all test methods in the class.

@Disabled("TODO: Still need to work on it")
@DisplayName("Division by zero")
@Test
void testIntegerDivision_WhenDividendIsDividedByZero_ShouldThrowArithmeticException() {
  
  // TODO: Still need to work on it
   
}

Another way

Another way to disable a test method is to simply comment out the @Test annotation.

 @DisplayName("Division by zero")
 // @Test
 void testIntegerDivision_WhenDividendIsDividedByZero_ShouldThrowArithmeticException() {
  // TODO: Still need to work on it
 }

If there is no test annotation, then it is not a test method. Methods that are not annotated with @Test annotation, will not be executed as JUnit test methods and will not be included in the test report.

Video Tutorial

I hope this tutorial was helpful to you. To learn more about how to test Java code with JUnit and Mockito, please have a look at the video course below.