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 ...

Get Test-Driven Java Development - Second Edition 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.