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.
Have a look at my previous posts, Passing Data Transfer Objects with GET in Spring Boot and Returning Data Transfer Objects from a Rest Controller in Spring Boot for information about how the DTOs are being passed to and returned from the Rest Controller. Also have a look at Spring’s starter guide if your starting from scratch. The setup that is not described in this post is covered there.
The Maven dependencies required in this post are
A little bit of context before we get to the tests.
Lets start with the simplest test.
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 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.
Firstly 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.
A closer look at one of the serialization tests.
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.
Now onto a deserialization test.
This is 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 a 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 is a slightly different way and then you can decided which you prefer.
This version removes the need to 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
was added to the @Befere method which 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.
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.
The code used in this post can be found on my GitHub.