In this post, I will cover some tests that can be run to ensure that your DTOs (Data Transfer Objects) are being serialized and deserialized correctly, leading onto testing whether they are being passed to and returned from a REST Controller without errors.

All this test does is check if the PersonRestController has been initialized. If it has not been loaded, then it will be null when it is injected in using the @Autowired annotation and lead to the test failing.

The next test will check if the PersonDTO will serialize and deserialize correctly:

Each property of the PersonDTO is tested for serialization and deserialization. If the serialization works correctly, then you can be sure that when you pass it to the REST controller in a JSON format, it will be received and converted to its proper object. You also want to ensure that the DTO can be deserialized correctly so it can be used when it is returned from the REST controller.

First, we mark the test with the @JsonTest annotation, which will disable full auto-configuration and only apply configuration relevant to JSON tests. This includes initializing the JacksonTester, which has been @Autowired into this test.

AssertJ is used in this test, as it provides useful methods for testing Spring applications. The JacksonTester converts the personDTO into JSON, and then the firstName property is extracted from it and compared to the expected value.

This test is basically the opposite of the previous test, but the configuration is almost the same. Instead of using json.write to serialize the object, json.parseObject turns the JSON into a PersonDTO.

The last test in this tutorial still tests serialization and deserialization, but also takes it a step further and involves the REST Controller.

The @WebMvcTest annotation is used, which will disable full auto-configuration and only apply configuration relevant to MVC tests, including setting up the MockMvc used in this test. The PersonRestController has been marked in the annotation, as it is the test subject. Using MockMvc provides a faster way of testing MVC controllers like the PersonRestController, as it removes the need to fully start an HTTP server.

Each test takes in a string that represents the request that is being sent to the PersonRestController and then checks that the request was sent and returned successfully and that the retrieved JSON is correct. I have included some of the static imports that were used to make it a bit clearer where some of these methods are coming from.

The downside to the way I have written this test is that there is a lot of setup of fiddly strings to be used in the test cases. This problem could also be seen as a benefit, as it is clear what the input is and what the output should be, but this up to preference. Therefore, I have written the same test in a slightly different way, and you can decide which you prefer.

This version removes the need for setting up the JSON strings by using JacksonTester to convert objects to JSON. Setting up this test is a bit nicer as you do not need to write all JSON strings which might have errors in them, such as missing a single speech mark or square bracket. Although I believe this also has the disadvantage of being less clear as you cannot see what the JSON strings are as it has been abstracted away by the JacksonTester.

A few things about the code setup of the second version. As the @WebMvcTest annotation is being used the @JsonTest that was used earlier cannot be applied. This prevents the JacksonTester from being injected into the test using @Autowired. Therefore, to set it up:

JacksonTester.initFields(this, objectMapper);

JacksonTester.initFields(this, objectMapper); was added to the @Befere method that runs at the start of each test method. If both @WebMvcTest and @JsonTest are added at the same time the test will fail to run.

Conclusion

In conclusion, this tutorial has gone through a few methods that can be used to test Data Transfer Objects and REST Controllers in Spring Boot to help ensure that your application is running correctly and removes the need for you to manually test every single case yourself.