Conditional Test Execution in JUnit 5

By | October 3, 2020

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

MyUtils.java

 

Operating System Conditions

A container or test may be enabled or disabled on a particular operating system using the @EnabledOnOs and @DisabledOnOs annotations.

 

 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.

 

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.

 

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.

 

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.

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.

Leave a Reply

Your email address will not be published.