Overriding an Autowired Bean in Unit Tests
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
When people ask how to override an autowired bean in a unit test, there are usually two different scenarios hiding underneath. Either the test is a true unit test and Spring should not be involved at all, or it is a Spring-backed test where the application context is loaded and one bean needs to be replaced.
The right answer depends on that distinction. For pure unit tests, prefer constructor injection and pass a fake or mock directly. For Spring tests, replace the bean with @MockBean, a test configuration, or a @Primary test bean.
Prefer Plain Unit Tests When You Can
If the class under test has only a few dependencies, the cleanest option is to avoid the Spring container entirely. Constructor injection makes that easy:
Then the test can provide a mock directly:
This is faster than loading Spring and is usually what "unit test" should mean.
Use @MockBean in Spring Test Contexts
If you really need Spring to create the object graph, @MockBean is usually the simplest override mechanism in Spring Boot tests:
@MockBean replaces the real bean in the application context with a Mockito mock. That makes it ideal for integration-style tests that still need Spring wiring.
Override with a Test Configuration
Sometimes a mock is not enough. You may need a lightweight fake implementation with predictable behavior. In that case, define a test configuration:
Then import it into the test:
The @Primary annotation ensures Spring prefers the test bean when both real and test versions exist.
Why Field Injection Makes Testing Harder
Bean overriding questions often come from code that uses field injection:
That style works, but it pushes you toward container-based tests. Constructor injection makes dependencies explicit and keeps both production code and tests easier to reason about.
Choose the Smallest Test Style That Fits
A useful rule is:
- pure unit test: instantiate the class manually
- Spring slice or integration test: use
@MockBean - custom fake dependency in Spring context: use test configuration plus
@Primary
The more Spring you involve, the slower and heavier the test becomes. Use that weight only when the test genuinely needs container behavior.
Common Pitfalls
- Calling something a unit test while loading the full Spring application context.
- Using field injection, which makes manual construction harder.
- Overriding beans with test configuration but forgetting
@Primary. - Mocking too much in a Spring test and turning it into a slow unit test.
- Reaching for Spring-based override mechanisms when constructor injection would avoid the problem entirely.
Summary
- In true unit tests, do not autowire; pass mocks or fakes through the constructor.
- In Spring Boot tests,
@MockBeanis the simplest way to replace an autowired dependency. - Use
@TestConfigurationand@Primarywhen you need a fake implementation instead of a mock. - Prefer constructor injection because it makes dependencies explicit and tests easier to write.
- Choose the lightest test style that still verifies the behavior you actually care about.
Related reading
- Overriding superclass property with different type in Swift
- Parameter 0 of constructor in ..... Spring Boot
- Parameter 0 of constructor in required a bean of type 'java.lang.String' that could not be found
- Pass An Instantiated System.Type as a Type Parameter for a Generic Class
- Overriding beans in Integration tests
- package org.springframework.boot does not exist
- Pass deep link into iOS Simulator?
- Passing Moq mock-objects to constructor

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.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.