How do we write tests that are easy to read, easy to write and run quickly? Alex Soto is speaking at Voxxed Days Vienna about how to write better unit tests. We asked him about his libraries of choice, and how to get the most out of tests.

What is your preferred Java testing library?

This is really a hard question to answer since there are a lot of libraries which cover different kind of tests. Some are specific to unit testing, others for component/integration tests or performance tests. The de-facto tool for Unit tests in Java is JUnit. This is the tool that I use most of the time. For component and integration tests I use the Arquillian ecosystem so the tests runs in a production-like, real testing environment. In case of performance tests, my preferred option is Gatling. This allows me to programmatically define performance scenarios. In all these cases, you are asserting that your program is doing things right (no bugs, corner cases, …). In case of doing BDD, and validating that the application does the right thing, then my preferred tool is Serenity BDD.

Obviously there are other testing libraries that can be used horizontally in all tests such as REST-Assured, AssertJ or Hoverfly. These are more than testing libraries – they are helper testing libraries that makes your tests easier to read and write.

What should you watch out for when writing unit tests?

Unit tests by definition mean that you test the smallest testable part of an application isolated from other parts. Usually in OOP languages, this smallest unit is considered to be a class.

The problem is that in normal cases, a class has dependencies to other classes, so to run tests in an isolated way, you need to use test doubles to replace these object dependencies.

What you need to watch out for is that sometimes the amount of effort of using test doubles is bigger than using the real code. For example when you use too many mocks, you don’t actually  test anything apart from the logic. So ideally, you need to decide if you want to use a test double approach, or just write sociable unit tests. Another option might be to just skip writing a unit test and write a component test.

What is the biggest challenge in writing unit tests?

One of the biggest challenges when writing tests, and specifically unit tests, are to make them readable and understandable for anyone with just a quick overview of the test. Any reader should be able to identify what a test is validating and what are the expectations on it.

Making tests clean means that anyone can understand what business code does through the unit tests. More importantly, anyone can collaborate on them.

How do we write better unit tests?

One of the easiest way to improve unit tests is just by clearly defining each of the steps of the test uses line separators and/or comments. Usually a test has three steps, the preparation of the test (or Given), the execution of the test (or When) and the validation (or Then).

Another quick win is by using AssertJ library for assertions. AssertJ allows you to write test assertions in a readable way for humans.


For more, see Alex’s talk at Voxxed Days Vienna.

voxxed days vienna


Testing for Unicorns

Profile photo of Katharine
About The Author
- Katharine is a Java developer by trade, turned Community & Content Manager for Voxxed. Helping developers learn and share knowledge. Contact me at with any news, articles or tutorials.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>