Testing Without Mocks: A Novel Approach to Unit Testing
This article presents a novel approach to unit testing that eliminates the need for mocks and spies, resulting in fast, reliable, and maintainable tests. The core idea involves combining state-based testing with a new infrastructure technique called "Nullables." Nullables aren't test doubles; they're production code with an "off" switch, allowing tests to run without relying on external systems or state. While requiring modifications to production code, this approach avoids many drawbacks of traditional methods, such as flaky tests and difficult refactoring, and offers significant performance gains. The article thoroughly explains the patterns and techniques involved, including Narrow Tests, State-Based Tests, and Overlapping Sociable Tests, providing ample code examples and diverse use cases, making it ideal for developers to learn and implement.