JSON Assertions for Spring MVC Tests

Paulo Freitas

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 jsonPath()


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!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade