Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand MockMvc role

    MockMvc simulates HTTP requests and responses in tests without needing a real server.
  2. Step 2: Compare options

    Options A, B, and D describe unrelated tasks like HTML generation, deployment, or database management.
  3. Final Answer:

    To test HTTP requests and responses without starting a real server -> Option A
  4. 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

  1. Step 1: Recall MockMvc syntax

    The correct syntax uses mockMvc.perform(get("/url")) with static import of get.
  2. Step 2: Check other options

    Options B, C, and D use invalid method names not provided by MockMvc API.
  3. Final Answer:

    mockMvc.perform(get("/api/data")) -> Option A
  4. 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?
mockMvc.perform(get("/hello"))
       .andExpect(status().isOk());
medium
A. 404 Not Found
B. 500 Internal Server Error
C. 200 OK
D. 302 Redirect

Solution

  1. Step 1: Understand status().isOk()

    The method status().isOk() checks for HTTP status code 200.
  2. Step 2: Match status codes

    200 means success, 404 means not found, 500 means server error, 302 means redirect.
  3. Final Answer:

    200 OK -> Option C
  4. Quick Check:

    status().isOk() = 200 OK [OK]
Hint: isOk() always means HTTP 200 success [OK]
Common Mistakes:
  • Confusing isOk() with error codes
  • Assuming isOk() means redirect
  • Mixing status codes meanings
4. Identify the error in this MockMvc test code snippet:
mockMvc.perform(post("/submit"))
       .andExpect(status().isOk)
       .andExpect(content().string("Success"));
medium
A. Using post instead of get for /submit
B. Missing parentheses after isOk method call
C. Incorrect content matcher method name
D. No error, code is correct

Solution

  1. Step 1: Check method calls

    The method isOk must be called with parentheses: isOk().
  2. Step 2: Verify other parts

    Using post for /submit is valid; content().string() is correct; so no other errors.
  3. Final Answer:

    Missing parentheses after isOk method call -> Option B
  4. 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

  1. Step 1: Understand JSON path syntax

    To check JSON fields, use jsonPath("$.fieldName") with value() for expected value.
  2. Step 2: Evaluate options

    andExpect(jsonPath("$.name").value("Alice")) uses correct syntax. andExpect(content().json("name: Alice")) has invalid JSON format. andExpect(jsonPath("name").isEqualTo("Alice")) uses wrong jsonPath syntax. andExpect(content().string("Alice")) checks whole string, not JSON field.
  3. Final Answer:

    andExpect(jsonPath("$.name").value("Alice")) -> Option D
  4. Quick Check:

    Use jsonPath("$.field").value() for JSON checks [OK]
Hint: Use jsonPath("$.field").value(expected) to check JSON fields [OK]
Common Mistakes:
  • Wrong jsonPath syntax without $
  • Trying to match JSON with plain string
  • Using incorrect matcher methods
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard