Learn to enable or disable the execution of the specific tests in JUnit 5 using inbuilt conditional execution annotations.
The inbuilt conditional annotations, that we can use to configure the conditions when a test should run and when it should not, are as follows.
1. @EnabledOnOs and @DisabledOnOs
- These annotations enable or disable the execution of the annotated test based on a particular operating system.
- The supported operating systems are listed in enum org.junit.jupiter.api.condition.OS are AIX, Linux, Mac, Solaris, Windows and Others.
- When applied at the class level, all test methods within that class will be enabled on the same specified operating systems.
- If a test method is disabled via this annotation, the test class will be initiated, only the test method and its life cycle methods will not be executed.
@Test
@EnabledOnOs(OS.MAC)
void testOnMacOs() {
assertTrue(true);
}
@Test
@DisabledOnOs(OS.WINDOWS)
void doNotTestOnWindows() {
assertTrue(true);
}
2. @EnabledOnJre and @DisabledOnJre
- These annotations can help in enabling or disabling the test for particular JRE version.
- The supported values can be found in the latest version of the JRE enum.
- If the current JRE version cannot be detected then none of the constants defined in JRE enum will be considered.
@Test
@DisabledOnJre(JRE.JAVA_8)
void disabledOnJava8() {
assertTrue(true);
}
@Test
@EnabledOnJre({ JRE.JAVA_17, JRE.JAVA_18 })
void enabledOnJava17Or18() {
assertTrue(true);
}
3. @EabledForJreRange and @DisabledForJreRange
- These annotations are used to signal that the annotated test class or test method is only disabled or enabled for a specific range of JRE versions using its
min
tomax
attributes. - When applied at the class level, all test methods within that class will be enabled on the same specified JRE versions.
@Test
@DisabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_11)
void notFromJava8to11() {
assertTrue(true);
}
@Test
@EnabledForJreRange(min = JRE.JAVA_12, max = JRE.JAVA_18)
void fromJava12to18() {
assertTrue(true);
}
4. @EnabledIf and @DisabledIf
- The annotated test class or test method is enabled or disabled only if the provided condition evaluates to true.
- When applied at the class level, all test methods within that class will be enabled or disabled on the same condition.
- When these annotations are used at class level, the condition method must always be
static
. - This annotation is not repeatable so can only be declared once.
@Test
@EnabledIf("customConditionalFunction")
void enabled() {
assertTrue(true);
}
@Test
@DisabledIf("customConditionalFunction")
void disabled() {
assertTrue(true);
}
boolean customConditionalFunction() {
return true;
}
5. @EnabledIfEnvironmentVariable and @DisabledIfEnvironmentVariable
- Use these annotations if we want to enable or disable our tests if the value of an environment variable matches the specified regular expression.
- When declared at the class level, the result will apply to all test methods within that class as well.
- If the specified environment variable is undefined, the presence of this annotation will have no effect.
- This is a repeatable annotation so it can be used multiple times on a test method or class.
@Test
@EnabledIfEnvironmentVariable(named = "ENV", matches = ".*development.*")
public void executeOnlyInDevEnvironment() {
return true;
}
@Test
@DisabledIfEnvironmentVariable(named = "ENV", matches = ".*prod.*")
public void disabledOnProdEnvironment() {
return true;
}
6. @EnabledIfSystemProperty and @DisabledIfSystemProperty
- Use these annotations if we want to enable or disable our tests if the value of the specified JVM system property matches the specified regular expression.
- When declared at the class level, the result will apply to all test methods within that class as well.
- If the specified system property is undefined, the presence of this annotation will have no effect.
- This is also a repeatable annotation so it can be used multiple times on a test method or class.
@Test
@EnabledIfSystemProperty(named = "any.system.property", matches = "value-regex*")
public void onlyIfPropertyValueIsFound() {
return true;
}
Happy Learning !!