Testing JSON Serialization With @JsonTest and Spring Boot

Last Updated:  August 27, 2021 | Published: March 17, 2020

Spring Boot provides a set of annotations you can use for writing tests for a specific part of your application. These annotations take care of auto-configuring Spring's ApplicationContext to include all beans you need. The most known ones are those of testing your web layer or database layer in isolation. There is also a lesser-known annotation for testing JSON serialization with either Jackson, Gson, or Jsonb: @JsonTest.

Spring Boot Project Setup

The demo project is using Java 11, Spring Boot 2.4, and two Spring Boot Starters:

The relevant Jackson dependencies are part of the Spring Boot Starter JSON, which is a transitive dependency of Spring Boot Starter Web:

Please note that all upcoming test examples use JUnit Jupiter (part of JUnit 5). If your application is using JUnit 4, add the @RunWith(SpringRunner.class) to each test class.

Test the JSON Serialization From Jackson

Let's start with a basic example and assume our codebase contains the following object to transfer data about a user:

As remote systems rely on the API contract, we should add a test to verify code changes in the future don't break the data format.

This example includes Jackson-specific annotations to manipulate the JSON representation of this class. Using @JsonFormat we can configure how to display the date and @JsonProperty manages the accessibility of the enabled field. JsonProperty.Access.WRITE_ONLY tells Jackson not to include the value while serializing the object but expect it when deserializing a JSON string back to this object.

All we need for the test setup with Spring Boot is the following:

The @JsonTest annotation ensures to auto-configure a Spring TestContext with only a subset of Spring Beans required to test the JSON serialization. Anything related to our web-layer or persistence-layer is not part of this sliced application context.

Besides the JacksonTester, there is also JsonbTester and GsonTester if your application is not using Jackson.

Next, let's write the actual test to verify the serialization to JSON:

Given the AssertJ JSON assertion support, we can verify the output by accessing the attributes with JSON Path expressions.

Furthermore, the JacksonTester can also be used to test the deserialization:

Testing JSON Serialization With Custom @JsonComponent Beans

As part of the auto-configuration triggered by @JsonTest, @JsonComponent beans are also included in the application context of the test. This annotation allows us to mark custom JsonSerializer or JsonDeserializer to be automatically picked up by Jackson's ObjectMapper.

As a quick demo for this, consider we have the following class:

And we use a custom JsonSerializer for serializing objects of this class:

We can test this without any further setup using @JsonTest:

Apart from @JsonTest, Spring Boot provides more annotations to test different parts of our application in isolation. You can find an overview of all common Spring Boot test slice annotations plus their usage and set up as part of this blog post.

You can find the source code for these JSON serialization test examples on GitHub.

Here's a list of further tutorials and resources to learn about Spring Boot's excellent test support:

Have fun testing your JSON serialization with @JsonTest,

Phil

    • Hi,

      thanks for your feedback. The cookie popup is a legal requirement and the only popup that should come in addition is on a leave intent. Are you referring to any other popups?

      Kind regards,
      Philip

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >