On a recent project, my team inherited a large, lightly-tested Java/Spring codebase. As we began to modify the code test-first, we ran into two common obstacles that prevent unit testing in Java:
- Class methods
- Objects instantiating other classes (using the
There are libraries that can help, but we wanted to tread lightly, so they weren’t an option. Instead, we decided to use the “subclass and override” technique from Michael Feathers’s very pragmatic legacy code book. Modifying code to make it more testable felt dirty at first, but the increase in test coverage, and consequently developer confidence, made it worth it.