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:

Hướng dẫn sử dụng Java Reflection
Different Ways to Capture Java Heap Dumps
Java Program to Create a Balanced Binary Tree of the Incoming Data
Entity To DTO Conversion for a Spring REST API
Thao tác với tập tin và thư mục trong Java
Introduction to Spring Boot CLI
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Một số nguyên tắc, định luật trong lập trình
Java Program to Perform Search in a BST
Java Program for Douglas-Peucker Algorithm Implementation
Spring Boot - Zuul Proxy Server and Routing
Hướng dẫn Java Design Pattern – Abstract Factory
Java – Byte Array to Reader
Jackson Exceptions – Problems and Solutions
Java Program to Generate Date Between Given Range
Java Program to Implement LinkedBlockingDeque API
Hướng dẫn Java Design Pattern – Command
The Guide to RestTemplate
JUnit5 Programmatic Extension Registration with @RegisterExtension
A Guide to Java 9 Modularity
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Guide to CountDownLatch in Java
How to Get a Name of a Method Being Executed?
Java Program to Implement Flood Fill Algorithm
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Set Interface trong Java
Java Program to Solve any Linear Equations
Spring WebClient and OAuth2 Support
Lớp Collections trong Java (Collections Utility Class)
Receive email by java client
Spring 5 and Servlet 4 – The PushBuilder