10 Unit testing

 

This chapter covers

  • Mistakes associated with assert statements
  • Misuse of assertion methods
  • Problems that happen when testing whether an exception is thrown
  • How a test case may succeed even if an assertion fails
  • Writing an incorrect test method that is not even executed

While unit testing is aimed at reducing the number of bugs in a program, the unit test itself is a program, so it’s natural that it may contain bugs, too. Some developers assume it’s not a big problem because if a unit test has a bug, then it will likely fail. However, I have seen many buggy unit tests that actually don’t test anything, so if the bug appears in the program, then the test won’t do its job of detecting the regression. In this chapter, we explore some bugs that can occur in unit tests.

Throughout this book, I’ve only discussed mistakes related to the Java language itself and its standard library. Here, I’ll make an exception and discuss a few mistakes one can make using testing frameworks, such as JUnit or TestNG. I believe this is an important part of the book. First, every Java program, be it a backend Spring application, a microservice, an Android project, a desktop game, or a general-purpose library, needs unit tests. Another point is that throughout the book, I often recommend writing unit tests to avoid or quickly identify certain mistakes. This advice only works if your unit tests are correctly written and actually executed during the test run.

10.1 Mistake 94: Side effect in assert statement

10.2 Mistake 95: Malformed assertion method calls

10.3 Mistake 96: Malformed exception test

10.4 Mistake 97: Premature exit from test method

10.5 Mistake 98: Ignoring the AssertionError in unit tests

10.6 Mistake 99: Using assertNotEquals() to check the equality contract

10.7 Mistake 100: Malformed test methods

Summary