O'Reilly logo

Test-Driven Java Development - Second Edition by Viktor Farcic, Alex Garcia

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Breaking dependencies

After having created a test that guarantees the expected behavior does not change, we will break the hardcoded dependency between BirthdayGreetingService and EmailMessageSender. For this, we will use a technique called extract and override call, which is first explained in Michael Feathers' book:

public class BirthdayGreetingService { 
 
  public BirthdayGreetingService() { 
    messageSender = getMessageSender(); 
  } 
 
  private MessageSender getMessageSender() { 
    return new EmailMessageSender(); 
  } 
 
[...] 

Execute the tests again and verify that the lonely test we previously created still is green. Additionally, we need to make this method protected or more open to be able to override it:

public class BirthdayGreetingService { protected ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required