Better Test Code Principles: #3 Use external libraries when available

Most of the testing tasks require some data processing, whether it is working with JSON objects, gathering data from a database, processing Strings, working with date types, and so on. Performing the processing requires some code to be written, apart from the tests themselves. But why write that code yourself, if it already exists in an easily usable external library?

In general, for Java tasks, there is a library for almost any kind of processing. How can you find out if there is a library for what you need? Some useful resources: the community (you can learn about such libraries by following testers on twitter or blogs, attending gatherings, meetups and conferences); subscribing to newsletters based on your interests; developers (yes, you can talk to your developers regarding what you are trying to do, and they will surely offer some guidance in that sense); stackoverflow will surely have some solution to the type of task you are trying to solve.

If the code you need to write, the helping code needed in your tests, is quick, easy and short, you could write it yourself. But when it is very complex, here are some reasons why you should just use an already existing one:

code used for testing is still code. It needs to be well thought, elaborated and tested. Good code must be checked to see how it behaves under various environment constraints. Is the code ready to handle the desired flows, but also exceptional ones? Have you though of all the possible ways this code will be used, and does it behave as it should under those scenarios?

writing the code properly might take a lot of time. Especially if it is complex. In this environment of rushing to deliver well tested features, the time required to write such code might not be justifiable.

code duplication and reinventing the wheel should be avoided.

What are some useful libraries? Take a look:

if you visit https://www.apache.org/, you will see a list of Apache projects, which are libraries used for all sorts of tasks. Some of them that can come in handy for testing include: