Chapter 11. Unit testing anti-patterns

 

This chapter covers

  • Unit testing private methods
  • Exposing private state to enable unit testing
  • Leaking domain knowledge to tests
  • Mocking concrete classes

This chapter is an aggregation of lesser related topics (mostly anti-patterns) that didn’t fit in earlier in the book and are better served on their own. An anti-pattern is a common solution to a recurring problem that looks appropriate on the surface but leads to problems further down the road.

You will learn how to work with time in tests, how to identify and avoid such anti-patterns as unit testing of private methods, code pollution, mocking concrete classes, and more. Most of these topics follow from the first principles described in part 2. Still, they are well worth spelling out explicitly. You’ve probably heard of at least some of these anti-patterns in the past, but this chapter will help you connect the dots, so to speak, and see the foundations they are based on.

11.1. Unit testing private methods

When it comes to unit testing, one of the most commonly asked questions is how to test a private method. The short answer is that you shouldn’t do so at all, but there’s quite a bit of nuance to this topic.

11.1.1. Private methods and test fragility

11.2. Exposing private state

11.3. Leaking domain knowledge to tests

11.4. Code pollution

11.5. Mocking concrete classes

11.6. Working with time

11.7. Conclusion

Summary

sitemap