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

Get 100 Java Mistakes and How to Avoid Them now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.