Using jsonPath() in MockMvc
Fast recall
Quick revision
Core answer
jsonPath() applies a JsonPath expression to the response JSON and lets MockMvc assert the selected value. $ is the document root, so jsonPath("$.name").value("Asha") checks one object field and jsonPath("$[0].id").value(10) checks the first array item. Pair content assertions with the expected status and content type.Practical knowledge of selecting and asserting JSON response fields with MockMvc
Only checking HTTP status codes without verifying response content, or not understanding JsonPath syntax like dollar-dot for fields and dollar-bracket-zero for array elements
Show object, nested, and array paths, distinguish a null value from an absent field, and choose JsonPath or DTO deserialization according to the contract being checked
Interview-ready guide
Interview answer
jsonPath() selects part of a JSON response and applies a MockMvc assertion. $ means the document root, so $.name checks a root property, $.address.city reaches a nested property, and $[0].id checks the first item in a root array. I pair those assertions with the expected status and content type. I use JsonPath when a few response fields define the scenario, and I can use exists(), doesNotExist(), array-size matchers, or value(...) as needed. If the complete typed response is important, deserializing it into a DTO can be clearer. The test should assert the public contract without depending on fields or ordering the API never promised.
Using jsonPath() in MockMvc
Direct Answer
jsonPath() applies a JsonPath expression to the response JSON and lets MockMvc assert the selected value. $ is the document root, so jsonPath("$.name").value("Asha") checks one object field and jsonPath("$[0].id").value(10) checks the first array item. Pair content assertions with the expected status and content type.
Code Example
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
@Test
void shouldReturnUserWithNestedAddress() throws Exception {
var user = new User(1L, "Alice", new Address("Seattle", "WA"));
when(userService.findById(1L)).thenReturn(Optional.of(user));
mockMvc.perform(get("/api/users/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1)) // Root field
.andExpect(jsonPath("$.name").value("Alice"))
.andExpect(jsonPath("$.address.city").value("Seattle")) // Nested object
.andExpect(jsonPath("$.address.state").value("WA"));
}
@Test
void shouldReturnOrdersArray() throws Exception {
var orders = List.of(
new Order(101L, "Laptop", 1200.00),
new Order(102L, "Mouse", 25.00)
);
when(userService.getOrders(1L)).thenReturn(orders);
mockMvc.perform(get("/api/users/1/orders"))
.andExpect(jsonPath("$").isArray()) // Assert it's an array
.andExpect(jsonPath("$[0].id").value(101)) // First element
.andExpect(jsonPath("$[1].item").value("Mouse")) // Second element
.andExpect(jsonPath("$.length()").value(2)); // Array size
}
}
Common Mistakes & Pitfalls
- Checking status only: Assert the stable response data that makes the scenario meaningful.
- Using the wrong root shape:
$.idselects an object property, while$[0].idstarts from an array. - Confusing null with absence: A present field whose value is null is different from a field that does not exist; choose the matching assertion.
- Over-specifying the response: Do not lock the test to incidental fields or array order that the API does not promise.
- Rejecting DTO mapping by rule: JsonPath suits selected fields; full deserialization can suit a complete typed contract.
Check your understanding
Answer these without looking back at the article.
- How do you assert that a JSON array in the response has exactly 3 items?
- What library does jsonPath() rely on and how is it included in Spring Boot tests?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.