top of page

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:

  1. 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?

  2. 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.

  3. code duplication and reinventing the wheel should be avoided. What are some useful libraries? Take a look:

  4. 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:

  5. 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.

  6. math: as the name suggests, math related operations: http://commons.apache.org/proper/commons-math/userguide/overview.html.

  7. collections: http://commons.apache.org/proper/commons-collections/.

  8. 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/)

  9. 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.

  10. 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.

Recent Posts

See All

Creating an Architecture for Your Automated Tests Writing Automated Tests in Small Increments 4 Times Your Automation Passed While Bugs Were Present Now That You’ve Created Automated Tests, Run Them!

This week my newest article was released, this time on how to use Log4j to log relevant test automation information: https://blog.testproject.io/2021/05/05/logging-test-automation-information-with-log

bottom of page