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:

Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Implement ConcurrentLinkedQueue API
Guide to Spring Cloud Kubernetes
Giới thiệu về Stream API trong Java 8
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Program to Implement a Binary Search Tree using Linked Lists
Java Program to Implement Knight’s Tour Problem
Limiting Query Results with JPA and Spring Data JPA
Spring Cloud Bus
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Custom HTTP Header with the HttpClient
Spring Boot Security Auto-Configuration
Java 8 Streams peek() API
Collect a Java Stream to an Immutable Collection
Hướng dẫn Java Design Pattern – Decorator
Java Program to Implement Park-Miller Random Number Generation Algorithm
Java Program to Check Cycle in a Graph using Graph traversal
Introduction to Spring Data JDBC
Changing Annotation Parameters At Runtime
Java Optional as Return Type
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to implement Dynamic Array
Chuyển đổi từ HashMap sang ArrayList
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Supplier trong Java 8
Java Program to Find Number of Articulation points in a Graph
Guide to the Java ArrayList
Migrating from JUnit 4 to JUnit 5
Java Program to Implement Adjacency List
Java Convenience Factory Methods for Collections
Returning Custom Status Codes from Spring Controllers