Tests and test-driven development (TDD) have arrived in most IT teams. Tests are supposed to assure the correct behavior of new and existing features, to survive refactorings unscathed, as documentation to make it easier for new developers to get started with the code, and – on top of that – lead to clean(er) code.
Unfortunately, many tests are not worth the time spent developing them. They are poorly readable and maintainable. And they often need adjustments during refactorings because they don't verify the result of an operation but how the operation was executed.
Bad Tests, Good Tests uses numerous examples (about half of the book contains code) to explain what constitutes bad tests – examples as the author has found them in real projects. The author then contrasts each example with specific suggestions for improvement.
The range of shortcomings goes from poor readability (e.g., long setup or assertion blocks) and obvious errors (e.g., forgotten assertions) to incomplete tests (that don't fail despite functionality changes) and subtle errors (e.g., exceptions thrown in places other than expected) to completely useless tests (that, e.g., verify the test library instead of your code).
The book is exceptionally hands-on and suitable for both beginners and advanced developers.
Beginners don't have to repeat the mistakes that others have made before them. Advanced programmers will surely recognize one or the other anti-pattern from their daily work and can fill their toolbox with new techniques.
The author writes understandably and humorously. And he explicitly points out that one may also break his rules if it serves the cause in the individual case.
A clear recommendation for every Java developer.
🎧 Suitable as an audiobook? No, due to countless code examples.
* Disclosure: We love sharing our favorite books with you! As an Amazon Associate, we earn a small commission from purchases you make through our links, which helps us continue creating content you enjoy.