0
0
Spring Bootframework~20 mins

MockMvc for HTTP assertions in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MockMvc Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
AHTTP 500 Internal Server Error
BHTTP 404 Not Found
CHTTP 200 OK
DHTTP 302 Found (Redirect)
Attempts:
2 left
💡 Hint
The test expects the status to be OK, so the controller must return 200.
📝 Syntax
intermediate
2: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'.
AmockMvc.perform(get("/api/item/1")).andExpect(jsonPath("$.name").value("Book"));
BmockMvc.perform(get("/api/item/1")).andExpect(jsonPath("name").value("Book"));
CmockMvc.perform(get("/api/item/1")).andExpect(jsonPath("$.name").equals("Book"));
DmockMvc.perform(get("/api/item/1")).andExpect(jsonPath("$.name").is("Book"));
Attempts:
2 left
💡 Hint
Use jsonPath with the correct JSON path syntax and the value() matcher.
🔧 Debug
advanced
2: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());
AThe controller does not have a mapping for /api/users/10
BThe MockMvc instance is not initialized
CThe test is missing a content type header
DThe test expects the wrong HTTP status
Attempts:
2 left
💡 Hint
404 means the URL was not found by the controller.
state_output
advanced
2: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();
A"<message>Hello World</message>"
B"Hello World"
C"{message: Hello World}"
D"{\"message\":\"Hello World\"}"
Attempts:
2 left
💡 Hint
The controller returns a JSON object with a message field.
🧠 Conceptual
expert
2: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.
AMockMvc requires the application to be deployed on a real server to work.
BMockMvc can test controller endpoints without starting the full HTTP server.
CMockMvc automatically tests database integration without configuration.
DMockMvc can only test GET requests, not POST or PUT.
Attempts:
2 left
💡 Hint
MockMvc simulates HTTP requests inside the test environment.