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 Solovay Strassen Primality Test Algorithm
Guide to @JsonFormat in Jackson
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Compact Strings in Java 9
Tạo số và chuỗi ngẫu nhiên trong Java
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Implement Disjoint Set Data Structure
Build a REST API with Spring and Java Config
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Spring – Injecting Collections
Java – Byte Array to Reader
Inject Parameters into JUnit Jupiter Unit Tests
Versioning a REST API
Java Program to Perform Addition Operation Using Bitwise Operators
Filtering a Stream of Optionals in Java
New Features in Java 14
Java Program to Perform LU Decomposition of any Matrix
Introduction to Spring MVC HandlerInterceptor
Spring’s RequestBody and ResponseBody Annotations
So sánh Array và ArrayList trong Java
Java Program to Implement the RSA Algorithm
Java – Get Random Item/Element From a List
Spring REST API + OAuth2 + Angular
Guide to the Volatile Keyword in Java
Java Program to Generate Random Numbers Using Probability Distribution Function
Java Program to Describe the Representation of Graph using Incidence Matrix
Java Program to Represent Graph Using Adjacency List
Limiting Query Results with JPA and Spring Data JPA
Spring Boot - Creating Docker Image
Jackson Ignore Properties on Marshalling
The Spring @Controller and @RestController Annotations
How to Find an Element in a List with Java