JUnit 5 @Disabled Test Example

JUnit @Disabled annotation can be used to disable the test methods from test suite. This annotation can be applied over a test class as well as over individual test methods.

It accepts only one optional parameter, which indicates the reason this test is disabled.

@Disabled Test Class

When @Disabled is applied over test class, all test methods within that class are automatically disabled as well.

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

@Disabled
public class AppTest {
	
	@Test
    void testOnDev() 
	{
		System.setProperty("ENV", "DEV");
        Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")));
    }
	
	@Test
    void testOnProd() 
	{
		System.setProperty("ENV", "PROD");
        Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")));
    }
}
JUnit 5 Disabled Annotation Over Class
JUnit 5 Disabled Annotation Over Class

Notice the count of Runs: 2/2 (2 skipped). Clearly both tests are disabled so not executed.

JUnit 5 @Disabled Test Method

@Disabled is used to signal that the annotated test method is currently disabled and should not be executed.

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

public class AppTest {
	
	@Disabled("Do not run in lower environment")
	@Test
    void testOnDev() 
	{
		System.setProperty("ENV", "DEV");
        Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")));
    }
	
	@Test
    void testOnProd() 
	{
		System.setProperty("ENV", "PROD");
        Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")));
    }
}
JUnit 5 Disabled Annotation Over Method
JUnit 5 Disabled Annotation Over Method

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.