Spring @Value List Injection: Version Differences and Testing Tips

This article explains how Spring’s @Value annotation injects List values differently from version 3.x through Spring Boot, highlights pitfalls such as immutable lists and delimiter issues, and provides practical testing strategies—including manual splitting, custom converters, and post‑construct adjustments—to ensure reliable configuration handling.

FunTester
FunTester
FunTester
Spring @Value List Injection: Version Differences and Testing Tips

In software testing, the Spring framework is frequently used for test development and automation, especially for correctly reading configuration files, which directly affects test case stability. The @Value annotation is commonly used to inject configuration values from files like application.properties, for example injecting user.type as a list of strings.

@Value("${user.type:FunTester1,FunTester8,FunTester9,FunTester10,FunTester7}")
private List<String> typeList;

This simple line hides differences in how various Spring versions handle List injection. Understanding these differences helps test engineers avoid configuration‑related failures.

Spring 3.x: Manual String Splitting

In Spring 3.x and earlier, @Value provides very limited support for List. If the configuration contains user.type=FunTester1,FunTester2,FunTester3, Spring injects the entire value as a single string instead of a list [FunTester1, FunTester2, FunTester3]. Test engineers must manually split the string using String.split(",") or define a custom converter.

Mitigation: Verify that the injected typeList is not a single string, test error configurations such as user.type=FunTester1;FunTester2 or an empty value user.type=, and ensure the system handles them gracefully.

Spring 4.0‑4.1: Automatic Splitting

Starting with Spring 4.0, a smarter type‑conversion mechanism automatically parses comma‑separated strings into List<String>. For example, FunTester1,FunTester2,FunTester3 becomes [FunTester1, FunTester2, FunTester3]. However, the injected list is immutable; attempting to add elements throws UnsupportedOperationException. Incorrect delimiters (e.g., semicolons) cause parsing failures.

Mitigation: In automated tests, create configuration scenarios with commas, semicolons, or spaces and verify the injection result. If a mutable list is needed, convert the immutable list to an ArrayList in a @PostConstruct method.

@Value("${user.type:FunTester1,FunTester8,FunTester9,FunTester10,FunTester7}")
private List<String> typeList;

@PostConstruct
public void init() {
    typeList = new ArrayList<>(typeList);
}

JUnit assertions can then verify the list size and contents, e.g., assertEquals(5, typeList.size()).

Spring 4.2+ and Spring Boot: More Intelligent Injection

From Spring 4.2 onward and in Spring Boot, @Value supports injection of List<String>, List<Integer>, and List<Enum>. Example:

@Value("${user.type:1,2,3}")
private List<Integer> typeList; // results in [1, 2, 3]

Spring Boot also supports YAML list configuration:

user:
  type:
    - FunTester1
    - FunTester2
    - FunTester3

Even with these improvements, the injected list remains immutable, and malformed configurations (extra spaces, illegal characters) can still cause exceptions.

Mitigation: Simulate configuration errors (e.g., user.type=,,) in performance or chaos testing, and add assertions to ensure typeList is non‑empty and contains valid values.

Test Engineer Pitfall Guide

Handle Immutable Lists: Convert injected List to a mutable ArrayList when tests need to modify it.

Watch for Configuration Format Errors: Test various delimiters (semicolon, space) and verify that the system either parses correctly or throws expected exceptions.

Avoid Duplicate Default Values: Define defaults only in @Value to prevent confusion between annotation defaults and field initialization.

Validate Injected Values: Add post‑construct checks to ensure the list is not empty and contains only legal entries, throwing an exception otherwise.

@Value("${user.type:FunTester1,FunTester8,FunTester9,FunTester10,FunTester7}")
private List<String> typeList;

@PostConstruct
public void validate() {
    if (typeList.isEmpty()) {
        throw new IllegalStateException("FunTester strategy list cannot be empty!");
    }
}

In automated tests, use assertions such as assertTrue(typeList.contains("FunTester1")) to confirm correct injection.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavatestingConfigurationspring@ValueList Injection
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.