We covered posting Json with Spring MVC in our last post. Now we’ll test it in JUnit with the new JacksonTester in Spring Boot Test 1.4.0. There are a number of things JacksonTester can do, but we’re going to use it to simply package our Json object when posting it to a Spring mockMVC controller. If we wanted to perform assertions we’d use AssertJ. There are good examples of using JacksonTester in the Spring Boot documentation. We could package a Json object for posting in a number of ways, but this is an opportunity to use something new in Spring Boot 1.4.
A conventional approach to sending Json would be to write it out and add it to the MVC post() with .content(data). Noisy Json.
A Better Test
Here’s the approach using JacksonTester .write().getJson(). Our jsonPostDTO object is a simplified PostDTO POJO containing only the fields used in the post object. A better test.