Spy Craft

In a way, the wrong turn helped me. Injecting a mock could have been an effective testing technique, but it would also couple the test to the implementation more tightly through the use of an internal type. This is the biggest danger in testing with mocks: the degree of coupling they introduce. [7f74b88] introduces a nicely uncoupled test, shown in Listing 14-4.

Listing 14-4: An implementation-independent test for the behavior of defaulting the protocol to HTTP

@Testpublic void testRetrieveResponse_DomainOnly()    throws IOException {  WebRetriever sut = new WebRetriever();  HttpResponse response =    sut.retrieveResponse("www.example.com");  assertThat(response, is(notNullValue()));}

The concept behind ...

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.