There is no doubt that any piece of software should have automated tests. Tests assure us that by adding a new functionality, we are not breaking anything that has been already done. Tests document the software. Tests decrease the amount of manual debugging. After reading a number of articles and books about automated tests, you can actually believe that automated tests are the panacea for all the evil in the software development world. And Java backend microservice application is no different.
Unfortunately, there is no magic in tests. Tests need to be well written to provide any value. Moreover, a misunderstood idea of automated tests can be even worse than having no tests at all, as they will trick you with false feedback.
How to prepare a proper suite of tests for a simple Java microservice?
First of all, we need to make sure that modules composing our application work well. Constraints, validation, specific business rules applied to your value objects are to be checked by unit tests. Also, all kinds of serializing, deserializing, marshaling, equals and hashcode contracts need to be tested.
Always use proper tools!
One of the most useful ones is EqualsVerifier. No matter how big the DTO class is, the hashcode and equal contract test are reduced to a couple of lines: