Changing Our Design to Support Testing

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? No, because it’s most important to demonstrate, in a simple fashion, that the system behaves the way you expect. Also, you have a better design: the dependency on Http is now declared in the clearest way possible, and moving the dependency to the ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.