0
0
Spring Bootframework~10 mins

MockMvc for HTTP assertions in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Adelete
Bpost
Cput
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get for fetching data.
Confusing HTTP methods.
2fill in blank
medium

Complete 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'
AMockMvcResultMatchers
BResultActions
CMockMvcBuilders
DMvcResult
Attempts:
3 left
💡 Hint
Common Mistakes
Using ResultActions instead of MockMvcResultMatchers.
Trying to assert status without the correct matcher.
3fill in blank
hard

Fix 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'
Acontains
BcontainsString
ChasString
DstringContains
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'contains' or 'hasString'.
Confusing with other matcher libraries.
4fill in blank
hard

Fill 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'
Apost
BMediaType.APPLICATION_JSON
CMediaType.TEXT_PLAIN
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for creating data.
Wrong content type like TEXT_PLAIN.
5fill in blank
hard

Fill 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'
A"$.name"
B"John"
C"$.id"
D"1"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong JSON path like '$.id' for the name field.
Not quoting string values properly.