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")));
}
}
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")));
}
}
Happy Learning !!
Comments