Populating Spring @Value during Unit Test
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
@Value fields are populated by the Spring container, not by plain Java object construction. That means the right testing strategy depends on what kind of test you are writing: a true unit test that instantiates the class directly, or a Spring-powered test that starts enough of the container to resolve properties.
In a Plain Unit Test, Spring Does Nothing
If you write:
then Spring is not involved, so fields annotated with @Value stay unset. That is expected. A plain unit test only sees normal Java behavior.
This is one reason constructor injection is often cleaner than field injection. You can still use @Value in the Spring-managed constructor, but the unit test can pass the value directly:
Plain unit test:
That is the fastest and simplest option when all you need is the configured value.
Use Spring Test Support When You Want Property Resolution
If the test is meant to verify Spring wiring itself, load a Spring context and provide the property through test configuration.
Example with @SpringBootTest:
This approach proves that Spring can resolve the placeholder and build the bean the way the application would.
You can also use @TestPropertySource when that style fits the project better:
Reflection Works, but It Is a Compromise
If a class still uses field injection and you want a very lightweight test, ReflectionTestUtils can set the field manually:
This is useful for legacy code, but it is usually a sign that constructor injection would make the class easier to test.
Choose the Smallest Test That Proves the Right Thing
Ask what the test is actually trying to prove:
- If you want to test business logic, pass the value directly and avoid starting Spring.
- If you want to test configuration and bean wiring, start Spring and provide test properties.
- If you are stuck with legacy field injection, reflection can bridge the gap.
Using a full application context for every class-level unit test slows the suite down and blurs the difference between unit tests and integration tests.
Common Pitfalls
The biggest mistake is expecting @Value to work in a plain unit test without Spring. It will not, because the container never ran.
Another common issue is loading the full Spring context just to test a small piece of logic that could have been exercised with direct constructor arguments.
Field injection is also a common source of testing friction. It hides dependencies and makes simple tests harder than they need to be.
Finally, property names in tests must match the placeholder exactly. If the bean expects client.base-url, setting client.url will not populate the field you care about.
Summary
- '
@Valueis resolved by Spring, not by plain object construction.' - For true unit tests, constructor injection usually makes the class easiest to test.
- Use
@SpringBootTestor@TestPropertySourcewhen you want Spring to resolve test properties. - '
ReflectionTestUtilscan help with legacy field injection, but it is not the cleanest long-term design.' - Pick the lightest test setup that proves the behavior you actually care about.
Related reading
- Possibly consider using a shorter maxLifetime value - hikari connection pool spring boot
- Postgres connection has been closed error in Spring Boot
- PostgreSQL row change notify java program and vice versa
- POSTing a OneToMany sub-resource association in Spring Data REST
- Possible to do a dry run validation of files?
- Practicing BDD with python
- Practical uses for AtomicInteger
- PreAuthorize not working on Controller

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.