0
0
Spring Bootframework~10 mins

@MockBean for mocking dependencies in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @MockBean for mocking dependencies
Start Test
Spring Context Loads
@MockBean Creates Mock
Inject Mock into Context
Test Runs Using Mock
Verify Behavior or Output
Test Ends
The test starts by loading Spring context, then @MockBean creates a mock object and injects it into the context, replacing the real bean. The test runs using this mock, allowing controlled behavior and verification.
Execution Sample
Spring Boot
@MockBean
private UserService userService;

@Test
void testGetUser() {
  when(userService.getUser()).thenReturn(new User("Alice"));
  String name = userService.getUser().getName();
  assertEquals("Alice", name);
}
This test mocks UserService, sets a return value for getUser(), calls it, and checks the returned name.
Execution Table
StepActionMock StateMethod CalledReturn ValueTest Assertion
1@MockBean creates mock UserServiceUserService mock creatednonenonenone
2when(userService.getUser()).thenReturn(new User("Alice"))Stub set for getUser()nonenonenone
3Call userService.getUser()Stub activegetUser()User("Alice")none
4Call getName() on returned UserStub activegetName()"Alice"none
5assertEquals("Alice", name)Stub activenonenonePass
6Test endsMock cleanednonenoneTest success
💡 Test ends after assertion passes using mocked UserService
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
userServicenullmock objectmock objectmock objectmock object
namenullnullUser("Alice")"Alice""Alice"
Key Moments - 2 Insights
Why does userService.getUser() return a User object even though userService is a mock?
Because at step 2 in the execution_table, we set a stub with when(...).thenReturn(...), telling the mock what to return when getUser() is called.
Does @MockBean replace the real bean in the Spring context?
Yes, as shown in the concept_flow, @MockBean creates a mock and injects it into the Spring context, replacing the real bean for the test.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does userService.getUser() return?
AUser("Alice") from the mock
BA real User from the database
Cnull
DAn exception
💡 Hint
Check the 'Return Value' column at step 3 in the execution_table.
At which step is the stub for getUser() set on the mock?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look for when(...).thenReturn(...) in the 'Action' column of the execution_table.
If we remove @MockBean, what would happen to the test?
ATest would still use the mock
BTest would fail to start Spring context
CTest would use the real UserService bean
DTest would pass without any changes
💡 Hint
Refer to the concept_flow about how @MockBean replaces the real bean in the context.
Concept Snapshot
@MockBean creates a mock of a Spring bean and injects it into the test context.
Use when(...).thenReturn(...) to define mock behavior.
The mock replaces the real bean during tests.
This allows isolated testing without real dependencies.
Assertions verify expected mock interactions or outputs.
Full Transcript
In Spring Boot tests, @MockBean creates a mock object of a bean and injects it into the Spring context, replacing the real bean. This lets tests control and verify the behavior of dependencies. The test sets up the mock's behavior using when(...).thenReturn(...). When the test calls methods on the mock, it returns the stubbed values. Assertions check these returned values to confirm correct behavior. This approach isolates the unit under test from real dependencies, making tests faster and more reliable.