Bird
0
0

What will happen if you run this MockMvc test?

medium📝 component behavior Q5 of 15
Spring Boot - Testing Spring Boot Applications
What will happen if you run this MockMvc test?
mockMvc.perform(get("/users/1"))
       .andExpect(jsonPath("$.name").value("Alice"));
ATest passes if the JSON response has a 'name' field with value 'Alice'
BTest passes if the JSON response contains any 'name' field
CTest fails if the JSON response has 'name' field with value 'Alice'
DTest fails if the JSON response is empty
Step-by-Step Solution
Solution:
  1. Step 1: Understand jsonPath assertion

    The jsonPath("$.name").value("Alice") checks that the JSON response has a 'name' field equal to 'Alice'.
  2. Step 2: Determine test pass condition

    If the response JSON matches this exactly, the test passes; otherwise, it fails.
  3. Final Answer:

    Test passes if the JSON response has a 'name' field with value 'Alice' -> Option A
  4. Quick Check:

    jsonPath value check = 'name' equals 'Alice' [OK]
Quick Trick: Use jsonPath("$.field").value(val) to check JSON content [OK]
Common Mistakes:
  • Ignoring exact value match in jsonPath
  • Assuming presence of field is enough without value check
  • Confusing jsonPath syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes