JUnit 5 @Disabled Test Example

JUnit @Disabled annotation is 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.

The @Disabled annotation accepts only one optional parameter, which indicates the reason this test is disabled.

To skip the execution of a test method, JUnit 4 uses the annotation @Ignore.

1. Using @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. Using @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

Comments

Subscribe
Notify of
guest
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.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode