0
0
Spring Bootframework~5 mins

MockMvc for HTTP assertions in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is MockMvc in Spring Boot testing?
MockMvc is a tool in Spring Boot that lets you test your web controllers by simulating HTTP requests and responses without starting a real server.
Click to reveal answer
beginner
How do you perform a GET request using MockMvc?
You use mockMvc.perform(get("/your-url")) to simulate a GET request to the specified URL.
Click to reveal answer
beginner
What method do you use to check the HTTP status in MockMvc assertions?
You use andExpect(status().isOk()) to check if the HTTP response status is 200 OK.
Click to reveal answer
intermediate
How can you verify the response content type with MockMvc?
Use andExpect(content().contentType("application/json")) to check if the response content type is JSON.
Click to reveal answer
intermediate
Why is MockMvc useful compared to starting a full server for tests?
MockMvc runs tests faster and isolates controller logic by simulating HTTP calls without starting the full web server, making tests quicker and easier to run.
Click to reveal answer
Which MockMvc method simulates an HTTP POST request?
Adelete("/url")
Bget("/url")
Cput("/url")
Dpost("/url")
How do you check if the response status is 404 Not Found in MockMvc?
AandExpect(status().isBadRequest())
BandExpect(status().isNotFound())
CandExpect(status().isOk())
DandExpect(status().isUnauthorized())
What does mockMvc.perform() return?
AThe full HTTP request object
BThe HTTP response body as a string
CA ResultActions object to chain assertions
DA boolean indicating success
Which annotation is commonly used to inject MockMvc in Spring Boot tests?
A@AutoConfigureMockMvc
B@MockBean
C@SpringBootApplication
D@Controller
How do you verify JSON content in the response using MockMvc?
AandExpect(content().json("{\"key\":\"value\"}"))
BandExpect(content().string("value"))
CandExpect(jsonPath("$.key").exists())
DandExpect(status().isJson())
Explain how MockMvc helps test Spring Boot controllers without starting a server.
Think about how you can test web calls without opening a browser or server.
You got /4 concepts.
    Describe the steps to write a MockMvc test that checks a GET request returns status 200 and JSON content.
    Start with performing the request, then add assertions.
    You got /4 concepts.