JUnit 5 @Disabled

JUnit @Disabled annotation can be used to exclude the test methods from the 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.

1. @Disabled on 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 and not executed.

2. @Disabled on Test Methods

@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

Happy Learning !!

Sourcecode Download

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.