Value Could not resolve placeholder in Spring Boot Test
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Could not resolve placeholder 'some.property' in value "${some.property}" error in Spring Boot tests means the @Value annotation references a property that is not defined in any property source available to the test context. This happens because test classes use a separate application context that may not load the same property files as the main application. The fix is to provide the property through @TestPropertySource, application-test.properties, or @SpringBootTest(properties = ...).
The Error
The test context cannot find app.api.key in any loaded property file.
Fix 1: @TestPropertySource (Recommended)
Provide test-specific properties directly on the test class:
Properties defined in @TestPropertySource override all other sources, making them ideal for test isolation.
Fix 2: Test Properties File
Create src/test/resources/application-test.properties:
Or use src/test/resources/application.properties (no profile) to override the main application properties for all tests.
Fix 3: @SpringBootTest properties
This is equivalent to @TestPropertySource(properties = ...) but defined on @SpringBootTest itself.
Fix 4: Default Values in @Value
Provide a fallback so the annotation never fails:
The syntax is ${property.name:defaultValue}. The colon separates the property name from the default.
Fix 5: Test Application Properties
Place a file at src/test/resources/application.properties:
Spring Boot's test resource classpath takes priority over main resources.
Understanding Property Source Priority
From highest to lowest priority in tests:
Common Scenarios
Missing Environment Variable
Properties from External Config Server
YAML Properties
If using application.yml, create src/test/resources/application-test.yml:
Slice Tests (@WebMvcTest, @DataJpaTest)
Slice tests load only part of the context, which often means fewer properties are available:
Always provide necessary properties explicitly in slice tests since they do not load the full application context.
Using @MockBean to Avoid Property Issues
If the property is only used by a bean you do not need in the test, mock the bean:
Common Pitfalls
- Typos in property names:
@Value("${app.api-key}")vsapp.api.keyin properties file. Spring Boot relaxed binding convertsapi-keytoapi.key, but@Valuedoes NOT use relaxed binding. Use@ConfigurationPropertiesfor relaxed binding support. - Wrong properties file location:
src/test/resources/application.propertiesis correct.src/test/application.properties(missingresources/) is ignored. - Profile not activated:
application-test.propertiesrequires@ActiveProfiles("test"). Without it, Spring only loadsapplication.properties. - SpEL confusion:
@Value("#{systemProperties['user.home']}")uses SpEL (Spring Expression Language, with#).@Value("${app.key}")uses property placeholder (with$). Mixing them up causes different errors. - Properties in @Configuration classes: If a
@Configurationclass uses@Valueand is loaded in the test context, you must provide those properties even if your test does not directly use them.
Summary
- Use
@TestPropertySource(properties = {"key=value"})for test-specific property overrides - Create
src/test/resources/application-test.propertieswith@ActiveProfiles("test")for shared test properties - Add default values with
@Value("${prop:default}")to prevent failures when properties are optional - Slice tests (
@WebMvcTest,@DataJpaTest) load minimal context, so always provide necessary properties explicitly - Use
@MockBeanto avoid resolving properties for beans you do not need in the test

