Spring MVC has a pretty awesome support for automated tests. Writing tests for my APIs is something that I really enjoy thanks to this incredible tool.
In this post I’d like to show a something very specific: JSON Assertions with JSON Path. That means checking the response of your API by verifying the resulting JSON structure.
Internally Spring uses Jayway JsonPath, and exposes it though the org.springframework.test.web.servlet.result.MockMvcResultMatchers#jsonPath method.
The code bellow performs a POST request and checks its response using
We expect to get a 200 OK and a JSON response object with a
data attribute containing a list with 6 elements.
Its that simple!