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")
✗ Incorrect
The post() method simulates an HTTP POST request in MockMvc.
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())
✗ Incorrect
Use status().isNotFound() to assert a 404 HTTP status.
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
✗ Incorrect
mockMvc.perform() returns a ResultActions object that allows chaining assertions like status and content checks.
Which annotation is commonly used to inject MockMvc in Spring Boot tests?
A@AutoConfigureMockMvc
B@MockBean
C@SpringBootApplication
D@Controller
✗ Incorrect
@AutoConfigureMockMvc sets up MockMvc for testing in Spring Boot.
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())
✗ Incorrect
Use content().json() to check if the response JSON matches the expected string.
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.
Practice
(1/5)
1. What is the main purpose of using MockMvc in Spring Boot tests?
easy
A. To test HTTP requests and responses without starting a real server
B. To deploy the application to a production server
C. To generate HTML pages automatically
D. To manage database connections
Solution
Step 1: Understand MockMvc role
MockMvc simulates HTTP requests and responses in tests without needing a real server.
Step 2: Compare options
Options A, B, and D describe unrelated tasks like HTML generation, deployment, or database management.
Final Answer:
To test HTTP requests and responses without starting a real server -> Option A
Quick Check:
MockMvc = HTTP testing without server [OK]
Hint: MockMvc tests HTTP calls without running the server [OK]
Common Mistakes:
Thinking MockMvc starts a real server
Confusing MockMvc with deployment tools
Assuming MockMvc manages databases
2. Which of the following is the correct way to perform a GET request using MockMvc?
easy
A. mockMvc.perform(get("/api/data"))
B. mockMvc.get("/api/data")
C. mockMvc.request("GET", "/api/data")
D. mockMvc.callGet("/api/data")
Solution
Step 1: Recall MockMvc syntax
The correct syntax uses mockMvc.perform(get("/url")) with static import of get.
Step 2: Check other options
Options B, C, and D use invalid method names not provided by MockMvc API.
Final Answer:
mockMvc.perform(get("/api/data")) -> Option A
Quick Check:
Use perform(get()) for GET requests [OK]
Hint: Use perform(get("url")) to send GET requests [OK]
Common Mistakes:
Using mockMvc.get() directly
Confusing method names like request() or callGet()
Not importing static get() method
3. Given the following test snippet, what HTTP status code will be asserted?
The method isOk must be called with parentheses: isOk().
Step 2: Verify other parts
Using post for /submit is valid; content().string() is correct; so no other errors.
Final Answer:
Missing parentheses after isOk method call -> Option B
Quick Check:
Method calls need parentheses like isOk() [OK]
Hint: Always add () after status check methods like isOk() [OK]
Common Mistakes:
Forgetting parentheses on isOk()
Confusing post and get methods
Using wrong content matcher method
5. You want to test a JSON response from /user/1 endpoint that returns {"name":"Alice","age":30}. Which MockMvc assertion correctly checks the name field in the JSON response?
hard
A. andExpect(content().string("Alice"))
B. andExpect(content().json("name: Alice"))
C. andExpect(jsonPath("name").isEqualTo("Alice"))
D. andExpect(jsonPath("$.name").value("Alice"))
Solution
Step 1: Understand JSON path syntax
To check JSON fields, use jsonPath("$.fieldName") with value() for expected value.