Testing Json Posting with Spring Boot JacksonTester

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.

Noisy Json

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.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.

Posted September 19, 2016 10:59 AM EDT

More Like This Post