Replace Collaborators

A collaborator is a component your software interacts with. For our purposes, we care about the components that the component under test uses to accomplish its goal. Let’s look at a reworked version of the previous NetRetriever example (Listing 8-8).

In a more realistic example, the connection would be managed separately from the NetRetriever. In addition to supporting the principle of separation of concerns, it abstracts the connection in a way that allows it to be reused and passed in from a larger context. While managing the open and close of the connection may not be the most neighborly or sporting approach in that design context, let’s tolerate it for now.

Listing 8-8: Listing 8-6 rewritten to pass the Connection

Get Quality Code: Software Testing Principles, Practices, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.