Challenge - 5 Problems
MockMvc Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this MockMvc test?
Given the following MockMvc test code, what will be the HTTP status returned by the test?
Spring Boot
mockMvc.perform(get("/api/items/5"))
.andExpect(status().isOk())
.andReturn();Attempts:
2 left
💡 Hint
The test expects the status to be OK, so the controller must return 200.
✗ Incorrect
The test uses andExpect(status().isOk()), which means it expects a 200 OK response from the controller for the GET request to /api/items/5.
📝 Syntax
intermediate2:00remaining
Which MockMvc code snippet correctly asserts JSON content?
Select the option that correctly asserts the JSON response contains a field 'name' with value 'Book'.
Attempts:
2 left
💡 Hint
Use jsonPath with the correct JSON path syntax and the value() matcher.
✗ Incorrect
Option A uses the correct JSON path syntax '$.name' and the value() matcher to check the field value. Other options use invalid methods or incorrect JSON path.
🔧 Debug
advanced2:00remaining
Why does this MockMvc test fail with a 404 error?
This MockMvc test fails with HTTP 404 Not Found. What is the most likely cause?
mockMvc.perform(get("/api/users/10"))
.andExpect(status().isOk());
Attempts:
2 left
💡 Hint
404 means the URL was not found by the controller.
✗ Incorrect
HTTP 404 means the requested URL is not mapped in the controller. The test fails because /api/users/10 is not handled by any controller method.
❓ state_output
advanced2:00remaining
What is the value of the response content after this MockMvc call?
Given this MockMvc call, what will be the exact response content string?
var result = mockMvc.perform(get("/api/message"))
.andExpect(status().isOk())
.andReturn();
String content = result.getResponse().getContentAsString();
Attempts:
2 left
💡 Hint
The controller returns a JSON object with a message field.
✗ Incorrect
The response content is a JSON string with escaped quotes: {"message":"Hello World"}. Option D matches the exact string including escaped quotes.
🧠 Conceptual
expert2:00remaining
Which statement about MockMvc and Spring Boot testing is true?
Choose the correct statement about using MockMvc for HTTP assertions in Spring Boot tests.
Attempts:
2 left
💡 Hint
MockMvc simulates HTTP requests inside the test environment.
✗ Incorrect
MockMvc allows testing controller endpoints by simulating HTTP requests without starting the full server. It does not require a real server and supports all HTTP methods.