Assert that JSON response string matches a certain class with Spring WebMvcTest

Viewed 11

Given a simple REST controller in Spring Boot...

@GetMapping("/user/{email}")
public UserResponse getUser(@PathVariable("email") String email) {
    return userService.getUserByEmail(email);
}

... I created a @WebMvcTest using MockMvc to assert the JSON content of the UserResponse object. Is it possible to also assert that the JSON string matches the UserResponse class itself (similar to Java's instance of)? I found the following but this does not work:

.andExpect(jsonPath("$", typeCompatibleWith(UserREsponse.class)))

0 Answers
Related