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:

Checked and Unchecked Exceptions in Java
Receive email using POP3
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement EnumMap API
Converting Between a List and a Set in Java
Spring Boot - Application Properties
Reactive WebSockets with Spring 5
Spring Boot - Hystrix
Java Program to Implement Attribute API
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Chuyển đổi Array sang ArrayList và ngược lại
Control Structures in Java
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Program to Implement Heap
Configure a Spring Boot Web Application
Java – InputStream to Reader
LinkedList trong java
Rate Limiting in Spring Cloud Netflix Zuul
Allow user:password in URL
Database Migrations with Flyway
Hướng dẫn Java Design Pattern – Bridge
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Reading an HTTP Response Body as a String in Java
Disable Spring Data Auto Configuration
Spring Boot With H2 Database
Prevent Cross-Site Scripting (XSS) in a Spring Application
Changing Annotation Parameters At Runtime
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Java Program to Implement the Monoalphabetic Cypher