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:
- lang: this is a very comprehensive library, having util classes for working with: booleans, enums, Strings, randomization, and so on. Check out the full list of its’ capabilities: http://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html.
- math: as the name suggests, math related operations: http://commons.apache.org/proper/commons-math/userguide/overview.html.
- collections: http://commons.apache.org/proper/commons-collections/.
- for DB work: i really like the JDBCTemplate from Spring (https://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html). There are other options out there, but this is by the far the easiest one to use. An alternative comes from Apache, in DbUtils (http://commons.apache.org/proper/commons-dbutils/)
- for working with date/time and are using a below 8 version of Java: Jodatime (http://www.joda.org/joda-time/) ; for versions above Java 8, they recommend to use java.time instead.
- a Google library for working with primitives, Strings, collections, and so on, is Guava (https://github.com/google/guava/wiki).
There are plenty of useful libraries out there, the Maven repository is full of them actually. You just need to find the right one for the task you need to solve.