Configure MockBean component before application start
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
@MockBean replaces or adds a bean in the Spring test ApplicationContext before the context is fully used by the test. That is usually enough when you only need a mock during request handling or ordinary bean interaction. The tricky case is when startup logic uses that bean before your test method runs. Then the mock must not only exist early, it must also be preconfigured early.
What @MockBean Does
In Spring Boot tests, @MockBean registers a Mockito mock in the test context and replaces an existing bean of the same type if one exists. That means the mock is part of the context, not just a local test variable.
A standard pattern looks like this:
This works well when the bean is only called after the test starts.
Why Startup Logic Changes the Problem
Suppose the application calls the dependency during:
- '
@PostConstruct' - '
ApplicationRunner' - '
CommandLineRunner' - eager bean initialization
In that case, stubbing the mock in @BeforeEach or the test method is too late. The mock exists, but it still returns Mockito defaults during startup.
That can lead to:
- '
nullvalues where real data was expected' - context startup failures
- confusing test behavior that looks like Spring timing issues
Use a Preconfigured Mock Bean
If startup code depends on the mock's behavior, define a test configuration that returns a pre-stubbed Mockito mock:
This works because the mock is already configured before dependent beans use it during context creation.
When @MockBean Is Still the Right Tool
If the dependency is not touched during startup, @MockBean is still simpler and usually preferred. It is concise, integrates cleanly with Spring Boot tests, and replaces the bean automatically.
For current Spring readers, there is one version note: newer Spring Framework testing support includes @MockitoBean, and current Spring Boot documentation marks @MockBean as deprecated in newer Boot lines. The underlying idea is the same: override a bean in the test context with a Mockito mock.
Alternative: Avoid Startup Work in the First Place
Sometimes the cleanest test fix is architectural. If a bean makes network calls or expensive operations during startup, ask whether that logic belongs there at all.
Moving the work out of @PostConstruct and into an explicit method often makes the application:
- easier to test
- easier to start locally
- less fragile in production
Test problems around early mocking are often a signal that the application is doing too much at startup.
Common Pitfalls
The biggest mistake is stubbing a @MockBean in @BeforeEach and expecting that stub to influence application startup. By then, startup has already happened.
Another mistake is using @MockBean when a simple property override or fake bean would be clearer. Not every test problem needs Mockito.
A third issue is forgetting that context caching can change how test setup behaves across classes. Keep mock configuration explicit and local to the test slice you need.
Summary
- '
@MockBeanregisters a mock in the Spring testApplicationContext.' - It is fine for normal test-time interactions, but startup logic can require earlier stubbing.
- If a bean is called during context startup, prefer a preconfigured
@TestConfigurationbean or equivalent override. - Newer Spring testing stacks also offer
@MockitoBeanfor similar bean override behavior. - If startup mocking is painful, reconsider whether the application should perform that work during startup at all.
Related reading
- Configure specific in memory database for testing purpose in Spring
- Configure Spring Boot with two ports
- Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error
- Configuring base package for component scan in Spring boot test
- Configuring IntelliJ IDEA for unit testing with JUnit
- Cordova start specific iOS emulator image
- Configuring ConnectionFactory for RabbitMQ in Spring Boot AMQP
- Configuring Log4j Loggers Programmatically

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.