JUnit Jupiter allows developers to either disable or enable a test based on certain conditions programmatically.
The org.junit.jupiter.api.condition package that allows developers to disable or enable containers and tests declaratively.
If you want to specify details about why they might be disabled, every annotation associated with these built-in conditions has a disabledReason attribute available for that purpose.
Let’s try to understand all concept using a demo project
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>ConditionalTestExecutionExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.7.0</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
MyUtils.java
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials; public class MyUtils { public int add(int n1, int n2) { return n1+n2; } } |
Operating System Conditions
A container or test may be enabled or disabled on a particular operating system using the @EnabledOnOs
and @DisabledOnOs
annotations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; public class OperatingSystemConditionsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test @EnabledOnOs(value = { OS.WINDOWS }) void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test @EnabledOnOs(value = { OS.WINDOWS,OS.LINUX }) void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test @DisabledOnOs(value = { OS.MAC }) void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } } |
Java Runtime Environment Conditions
A container or test may be enabled or disabled on particular versions of the Java Runtime Environment (JRE) using the @EnabledOnJre
and @DisabledOnJre
annotations or on a particular range of versions of the JRE using the @EnabledForJreRange
and @DisabledForJreRange
annotations. The range defaults to JRE.JAVA_8
as the lower border (minimum
) and JRE.OTHER
as the higher border (maximum
), which allows the usage of half-open ranges.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.EnabledOnJre; import org.junit.jupiter.api.condition.JRE; public class JavaRuntimeEnvironmentConditionsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test @EnabledOnJre(value = { JRE.JAVA_8 }) void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test @EnabledOnJre(value = { JRE.JAVA_8,JRE.JAVA_9 }) void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test @EnabledForJreRange(min = JRE.JAVA_8,max = JRE.JAVA_13) //@DisabledOnJre(value = { JRE.JAVA_10,JRE.JAVA_13 }) void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } } |
System Property Conditions
A container or test may be enabled or disabled based on the value of the named
JVM system property using the @EnabledIfSystemProperty
and @DisabledIfSystemProperty
annotations. The value supplied via the matches
an attribute will be interpreted as a regular expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIfSystemProperty; import org.junit.jupiter.api.condition.EnabledIfSystemProperty; public class SystemPropertyConditionsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test @EnabledIfSystemProperty(named = "java.vm.vendor", matches = "Oracle.*") void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test @DisabledIfSystemProperty(named = "ci-server", matches = "true") void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } } |
Environment Variable Conditions
A container or test may be enabled or disabled based on the value of the named
environment variable from the underlying operating system using the @EnabledIfEnvironmentVariable
and @DisabledIfEnvironmentVariable
annotations. The value supplied via the matches
an attribute will be interpreted as a regular expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; public class EnvironmentVariableConditionsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); System.setProperty("ENV", "staging-server"); System.out.println("Test data set up is done.."); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test @EnabledIfEnvironmentVariable(matches = "staging-server", named = "ENV") void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test @DisabledIfEnvironmentVariable(named = "ENV", matches = ".*dev.*") void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown() { myUtils = null; System.out.println("Test data teardown is done.."); } } |
Custom Conditions
A container or test may be enabled or disabled based on the boolean return of a method via the @EnabledIf
and @DisabledIf
annotations. The method is provided to the annotation via its name, or it’s the fully qualified name if located outside the test class. Sometimes you need the condition method that takes a single parameter of type ExtensionContext
.
When @EnabledIf
or @DisabledIf
is used at class level, the condition method must always be static
. Condition methods defined in external classes must also be static
. In any other case, you may use both static or instance methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.kkjavatutorials; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIf; import org.junit.jupiter.api.condition.EnabledIf; public class CustomConditionsTest { @Test @EnabledIf("customCondition") void enabled() { System.out.println("enabled:Test execution depends on customCondition"); } @Test @DisabledIf("customCondition") void disabled() { System.out.println("disabled:Test execution depends on customCondition"); } boolean customCondition() { return true; } } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
That’s all about Conditional Test Execution in JUnit 5
If you have any feedback or suggestion please feel free to drop in below comment box.