How to test Classes with ConfigurationProperties and Autowired
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Testing classes that use @ConfigurationProperties and @Autowired works best when you separate two concerns: property binding and business logic. One test should verify that Spring binds configuration correctly, and another should verify that your class behaves correctly once those dependencies are injected.
Prefer Constructor Injection First
The easiest classes to test are the ones that use constructor injection. Spring can still autowire them in production, but plain unit tests can instantiate them directly with mocks or test values.
That design gives you a clean seam for both Spring tests and plain unit tests.
Test Property Binding in a Small Spring Context
You do not always need @SpringBootTest. For focused property-binding tests, a small context is faster and clearer.
This verifies the binding rules without loading your whole application.
Test the Service as a Plain Unit Test
Once property binding is verified separately, test the service without Spring.
This kind of test is faster than starting a Spring context and it fails for business-logic reasons, not framework wiring noise.
When @SpringBootTest Is Appropriate
Use @SpringBootTest when you genuinely need integration across several auto-configured beans, profiles, converters, or externalized configuration layers.
This is heavier, but useful when you want confidence that the real wiring path works.
Avoid Field Injection in New Code
Field injection is testable, but it is harder to reason about and encourages reflection-based setup in unit tests. Constructor injection makes dependencies explicit and reduces the need for Spring in simple tests.
If you inherit field-injected code, you can still test it with @SpringBootTest or ReflectionTestUtils, but that is usually a sign the class design can be improved.
Common Pitfalls
The biggest mistake is using @SpringBootTest for every class test. That makes the suite slower and blurs the difference between binding problems and business-logic problems.
Another issue is testing @ConfigurationProperties only indirectly through service behavior. When binding and logic are mixed in one test, failures become harder to diagnose.
Developers also forget to enable the configuration-properties class in narrow tests. If Spring never registers the properties bean, the test does not prove anything about binding.
Finally, field injection makes unit testing more awkward than it needs to be. Prefer constructor injection so dependencies are explicit and easy to supply.
Summary
- Test property binding and service logic as separate concerns.
- Use constructor injection to keep classes easy to instantiate in unit tests.
- Prefer small context tests such as
ApplicationContextRunnerfor@ConfigurationProperties. - Use
@SpringBootTestonly when full integration is the thing you actually want to verify. - Avoid field injection in new code because it makes tests less direct.
Related reading
- How to test code dependent on environment variables using JUnit?
- How to test main class of Spring-boot application
- How to test methods that call System.exit?
- How to test methods that call System.exit?
- How to test credentials for AWS Command Line Tools
- How to test equality of Swift enums with associated values
- How to test Spring Scheduled
- How to train image pixel data in libsvm format to use for recognition with Java

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.