Bird
0
0

You want to test a controller that returns a list of users as JSON. Which MockMvc assertion verifies the JSON array size is 3?

hard📝 component behavior Q8 of 15
Spring Boot - Testing Spring Boot Applications
You want to test a controller that returns a list of users as JSON. Which MockMvc assertion verifies the JSON array size is 3?
AandExpect(jsonPath("$.length()", is(3)))
BandExpect(jsonPath("$.size", equalTo(3)))
CandExpect(jsonPath("$", hasSize(3)))
DandExpect(jsonPath("$.count()", is(3)))
Step-by-Step Solution
Solution:
  1. Step 1: Understand jsonPath for array size

    To check the size of a JSON array, use jsonPath("$", hasSize(n)).
  2. Step 2: Identify correct matcher

    hasSize(3) correctly asserts the array length is 3.
  3. Final Answer:

    Use andExpect(jsonPath("$", hasSize(3))) to verify array size -> Option C
  4. Quick Check:

    JSON array size check = jsonPath("$", hasSize(n)) [OK]
Quick Trick: Use jsonPath("$", hasSize(n)) to check JSON array length [OK]
Common Mistakes:
  • Using length() or count() which are invalid in jsonPath
  • Using $.size which is not standard
  • Confusing matchers like is() with hasSize()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes