@MockBean vs @Mock Spring Boot | Java Fresher Interview
Fast recall
Quick revision
Core answer
@Mock creates a standalone Mockito mock and does not add it to Spring. The older Spring Boot @MockBean replaces or adds a bean inside a test ApplicationContext; since Spring Boot 3.4 it is deprecated for removal in Boot 4.0 in favor of Spring Framework's @MockitoBean. Use @Mock for a plain unit test and a bean override when a Spring test such as @WebMvcTest needs the mock in context-managed wiring.Interview-ready guide
Interview answer
@Mock creates a Mockito mock outside Spring.
I use it in a unit test, construct the service myself, and pass the mock through the constructor. A Spring bean-override annotation is different: it puts the mock into the test ApplicationContext, so controllers or services created by Spring receive it. 0, and current code uses Spring Framework's @MockitoBean.
class)can declare@MockitoBean OrderService servicebecause Spring creates the controller. A plainOrderServicetest normally needs only@Mock PaymentGateway` and direct constructor wiring. Different bean overrides can also create different context-cache keys, so I keep them consistent rather than claiming that each annotation simply invalidates the whole cache.
@MockBean vs @Mock Spring Boot | Java Fresher Interview
Direct Answer
@Mock creates a standalone Mockito mock and does not add it to Spring. The older Spring Boot @MockBean replaces or adds a bean inside a test ApplicationContext; since Spring Boot 3.4 it is deprecated for removal in Boot 4.0 in favor of Spring Framework's @MockitoBean. Use @Mock for a plain unit test and a bean override when a Spring test such as @WebMvcTest needs the mock in context-managed wiring.
Code Example
// Plain unit test: no Spring context
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
@Mock PaymentGateway gateway;
@Test
void rejectsDeclinedPayment() {
var service = new OrderService(gateway);
when(gateway.charge(100)).thenReturn(false);
assertThrows(PaymentDeclinedException.class,
() -> service.placeOrder(100));
}
}
// Spring MVC slice: mock is registered in the test context
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired MockMvc mvc;
@MockitoBean OrderService service;
}
Common Mistakes & Pitfalls
- Using the wrong lifecycle:
@Mockneeds Mockito initialization; a Spring bean override needs a Spring TestContext. - Confusing context loading with mocking:
@MockBeandoes not by itself turn a plain unit test into a Spring test. - Unnecessary context tests: if
new OrderService(mockGateway)is enough, a plain Mockito test is clearer. - Ignoring current APIs: on current Spring versions, prefer
@MockitoBean;@MockBeanis the older Boot annotation. - Fragmenting the context cache: inconsistent override declarations across test classes can reduce reuse of cached contexts.
Check your understanding
Answer these without looking back at the article.
- What is @WebMvcTest and how do you use @MockBean with it?
- What is context caching in Spring Boot tests?
- What is @SpyBean?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.