0
0
Spring Bootframework~10 mins

@WebMvcTest for controller testing in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @WebMvcTest for controller testing
Start Test
Load @WebMvcTest Context
Inject MockMvc
Perform HTTP Request
Controller Handles Request
Return Response
Assert Response & Status
Test Ends
The test starts by loading only the web layer with @WebMvcTest, then MockMvc performs a simulated HTTP request to the controller, which handles it and returns a response that the test asserts.
Execution Sample
Spring Boot
@WebMvcTest(MyController.class)
class MyControllerTest {
  @Autowired
  MockMvc mockMvc;

  @Test
  void testHello() throws Exception {
    mockMvc.perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get("/hello")).andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status().isOk());
  }
}
This test loads only MyController, performs a GET request to /hello, and checks if the response status is 200 OK.
Execution Table
StepActionComponentResultNotes
1Start testTest runnerTest context startsTest framework initializes
2Load @WebMvcTest contextSpring BootOnly web layer loadedNo full app context
3Inject MockMvcTest classMockMvc instance readyUsed to simulate HTTP calls
4Perform GET /helloMockMvcRequest sent to controllerSimulates browser call
5Controller handles requestMyControllerProcesses /helloRuns controller method
6Return responseMyControllerHTTP 200 OKResponse ready for test
7Assert response statusTestStatus is 200 OKTest passes if true
8Test endsTest runnerTest completesNo errors
💡 Test ends after asserting the HTTP response status is OK
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
mockMvcnullMockMvc instanceUsed to send GET /helloReceived responseReady for next test
responseStatusN/AN/AN/A200 OK200 OK
Key Moments - 3 Insights
Why does @WebMvcTest load only the web layer and not the full application?
Because @WebMvcTest is designed to test controllers in isolation, it loads only the web-related beans like controllers, filters, and MVC config, skipping services and repositories. This is shown in execution_table step 2.
How does MockMvc simulate an HTTP request without a real server?
MockMvc performs requests inside the test context by calling controller methods directly, without starting a server. This is shown in execution_table steps 4 and 5.
What happens if the controller returns a different status than expected?
The assertion in step 7 will fail, causing the test to fail. This ensures the controller behaves as expected.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the MockMvc instance created?
AStep 5
BStep 3
CStep 2
DStep 1
💡 Hint
Check the 'Action' and 'Result' columns in execution_table row for step 3
At which step does the controller actually process the HTTP request?
AStep 5
BStep 4
CStep 6
DStep 7
💡 Hint
Look for 'Controller handles request' in execution_table
If the controller returned HTTP 404 instead of 200, which step would detect this?
AStep 5
BStep 6
CStep 7
DStep 4
💡 Hint
The assertion of response status happens in step 7
Concept Snapshot
@WebMvcTest loads only the web layer for controller testing.
MockMvc simulates HTTP requests without starting a server.
Use @WebMvcTest(MyController.class) to test one controller.
Perform requests with mockMvc.perform() and assert results.
This isolates controller logic from services and repositories.
Full Transcript
This visual execution trace shows how @WebMvcTest works for controller testing in Spring Boot. The test starts by loading only the web layer, not the full application context. Then, MockMvc is injected to simulate HTTP requests. When mockMvc.perform(get("/hello")) runs, it sends a fake GET request to the controller. The controller processes the request and returns an HTTP 200 OK response. The test asserts this status to verify correct behavior. This approach isolates the controller and speeds up tests by avoiding full context loading.