Spring 5 Testing with @EnabledIf Annotation

1. Introduction

In this quick article, we’ll discover the @EnabledIf and @DisabledIf annotations in Spring 5 using JUnit 5.

Simply put, those annotations make it possible to disable/enable particular test if a specified condition is met.

We’ll use a simple test class to show how these annotations work:

@SpringJUnitConfig(Spring5EnabledAnnotationIntegrationTest.Config.class)
public class Spring5EnabledAnnotationIntegrationTest {
 
    @Configuration
    static class Config {}
}

2. @EnabledIf

Let’s add to our class this simple test with a text literal “true”:

@EnabledIf("true")
@Test
void givenEnabledIfLiteral_WhenTrue_ThenTestExecuted() {
    assertTrue(true);
}

If we run this test, it executes normally.

However, if we replace the provided String with “false” it’s not executed:

Keep in mind that if you want to statically disable a test, there’s a dedicated @Disabled annotation for this.

3. @EnabledIf With a Property Placeholder

A more practical way of using @EnabledIf is by using a property placeholder:

@Test
@EnabledIf(
  expression = "${tests.enabled}", 
  loadContext = true)
void givenEnabledIfExpression_WhenTrue_ThenTestExecuted() {
    // ...
}

First of all, we need to make sure that the loadContext parameter is set to true so that the Spring context gets loaded.

By default, this parameter is set to false to avoid unnecessary context loading.

4. @EnabledIf With a SpEL Expression

Finally, we can use the annotation with Spring Expression Language (SpEL) expressions.

For example, we can enable tests only when running JDK 1.8

@Test
@EnabledIf("#{systemProperties['java.version'].startsWith('1.8')}")
void givenEnabledIfSpel_WhenTrue_ThenTestExecuted() {
    assertTrue(true);
}

5. @DisabledIf

This annotation is the opposite of @EnabledIf.

For example, we can disable test when running on Java 1.7:

@Test
@DisabledIf("#{systemProperties['java.version'].startsWith('1.7')}")
void givenDisabledIf_WhenTrue_ThenTestNotExecuted() {
    assertTrue(true);
}

6. Conclusion

In this brief article, we went through several examples of the usage of @EnabledIf and @DisabledIf annotations in JUnit 5 tests using the SpringExtension.

The full source code for the examples is available over on GitHub.

Related posts:

Spring Boot - File Handling
Java – Delete a File
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Implement the Program Used in grep/egrep/fgrep
Java Program to Implement Segment Tree
Getting Started with Custom Deserialization in Jackson
Java Program to implement Sparse Vector
Handling URL Encoded Form Data in Spring REST
Hướng dẫn Java Design Pattern – Visitor
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
How to Use if/else Logic in Java 8 Streams
Java Program to Implement Best-First Search
Java Program to Implement Gaussian Elimination Algorithm
Check If a File or Directory Exists in Java
Java Program to Implement Jarvis Algorithm
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Sorting in Java
Java Program to Implement ScapeGoat Tree
Java Program to Implement PrinterStateReasons API
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Spring Security and OpenID Connect
So sánh Array và ArrayList trong Java
Intro to Spring Boot Starters
The Difference Between Collection.stream().forEach() and Collection.forEach()
Working with Network Interfaces in Java
A Guide to Java HashMap
Java Program to Perform Searching Based on Locality of Reference
Practical Java Examples of the Big O Notation
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Perform the Shaker Sort
Assertions in JUnit 4 and JUnit 5
Java Program to Check Multiplicability of Two Matrices