Changing Our Design to Support Testing
Explore how modifying system design facilitates better unit testing in Java applications by enabling dependency injection of mocks. Discover various techniques for injecting stubs, such as constructors, setters, and factory overrides. Understand how to create smarter stubs that verify method parameters to catch defects early. This lesson helps you build more reliable tests by ensuring the correctness of interactions with dependencies like HTTP clients.
We'll cover the following...
Our new code represents a small change to the design of the system. Before, the Http instance was created by the retrieve() method as a private detail of the AddressRetriever class. Now, any client that interacts with AddressRetriever is responsible for creating and passing in an appropriate Http instance. Perhaps something like:
AddressRetriever retriever = new AddressRetriever(new HttpImpl());
Is changing your system’s design just to write a test a bad thing?