Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to perform a GET request using MockMvc.
Spring Boot
mockMvc.perform([1]("/api/data"))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get for fetching data.
Confusing HTTP methods.
✗ Incorrect
The get method is used to perform a GET request in MockMvc.
2fill in blank
mediumComplete the code to expect a 200 OK status in the response.
Spring Boot
mockMvc.perform(get("/api/data")).andExpect([1].status().isOk())
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ResultActions instead of MockMvcResultMatchers.
Trying to assert status without the correct matcher.
✗ Incorrect
MockMvcResultMatchers provides methods to assert the response status and content.
3fill in blank
hardFix the error in the code to check the response content contains 'success'.
Spring Boot
mockMvc.perform(get("/api/data")).andExpect(MockMvcResultMatchers.content().string([1]("success")))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'contains' or 'hasString'.
Confusing with other matcher libraries.
✗ Incorrect
The method containsString is used to check if the response content includes a specific substring.
4fill in blank
hardFill both blanks to perform a POST request with JSON content and expect a 201 status.
Spring Boot
mockMvc.perform([1]("/api/create").contentType([2]).content(jsonData)).andExpect(MockMvcResultMatchers.status().isCreated())
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for creating data.
Wrong content type like TEXT_PLAIN.
✗ Incorrect
Use post to send data and MediaType.APPLICATION_JSON to specify JSON content type.
5fill in blank
hardFill both blanks to check JSON response field 'name' equals 'John'.
Spring Boot
mockMvc.perform(get("/api/user/1")).andExpect(MockMvcResultMatchers.jsonPath([1]).value([2]))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong JSON path like '$.id' for the name field.
Not quoting string values properly.
✗ Incorrect
The JSON path "$.name" targets the 'name' field, and "John" is the expected value.