0
0
Spring Bootframework~10 mins

MockMvc for HTTP assertions in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MockMvc for HTTP assertions
Setup MockMvc with WebApplicationContext
Build HTTP request (GET/POST/etc)
Perform request with MockMvc
Receive MockHttpServletResponse
Assert status, headers, body content
Test passes or fails based on assertions
MockMvc simulates HTTP requests to Spring controllers and lets you check the response without running a server.
Execution Sample
Spring Boot
mockMvc.perform(get("/hello"))
       .andExpect(status().isOk())
       .andExpect(content().string("Hello World"));
This code sends a GET request to /hello and checks if the response status is 200 OK and the body is 'Hello World'.
Execution Table
StepActionRequest DetailsResponse StatusResponse BodyAssertion Result
1Build GET requestGET /hello--Request ready
2Perform requestSend GET /hello200Hello WorldResponse received
3Assert statusExpect 200 OK200Hello WorldPass
4Assert contentExpect body 'Hello World'200Hello WorldPass
5Test result-200Hello WorldTest passes
💡 All assertions passed, test completes successfully
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
mockMvcinitializedinitializedinitializedinitializedinitializedinitialized
requestnullGET /helloGET /helloGET /helloGET /helloGET /hello
responseStatusnullnull200200200200
responseBodynullnullHello WorldHello WorldHello WorldHello World
assertionsnonenonenonestatus OK passedcontent passedall passed
Key Moments - 3 Insights
Why do we not need to start a real server to test HTTP requests?
MockMvc simulates HTTP requests inside the test environment, so no real server is needed. See execution_table step 2 where the request is performed but no actual network call happens.
What happens if the response status is not what we expect?
The assertion in execution_table step 3 would fail, causing the test to fail immediately, preventing further assertions.
Can we test POST requests with MockMvc the same way?
Yes, you build a POST request instead of GET in step 1 and perform it similarly. The flow remains the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response status after performing the request?
A500
B404
C200
Dnull
💡 Hint
Check the 'Response Status' column at step 2 in the execution_table.
At which step does the test confirm the response body content?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Assertion Result' columns in execution_table for step 4.
If the response body was 'Hello Spring' instead of 'Hello World', what would happen at step 4?
AAssertion passes
BAssertion fails
CTest skips this step
DResponse status changes
💡 Hint
Step 4 asserts the exact response body string; a mismatch causes failure.
Concept Snapshot
MockMvc lets you test Spring MVC controllers without a server.
Build a request (GET, POST, etc) with mockMvc.perform().
Check response status and content with andExpect().
Assertions fail test if response differs.
Fast, isolated HTTP testing inside your test code.
Full Transcript
MockMvc is a tool in Spring Boot that lets you simulate HTTP requests to your controllers without starting a real server. You create a request like GET or POST using mockMvc.perform(), then check the response status and body using assertions like andExpect(status().isOk()) and andExpect(content().string(...)). The execution flow starts by building the request, performing it, receiving a mock response, and then asserting expected results. If any assertion fails, the test fails immediately. This allows fast and reliable testing of your web layer in isolation.