Finding test points

As the BirthdayGreetingService class does not have a collaborator injected that could be used to attach additional responsibilities to the object, the only option is to go outside this service class to test it. An option would be to change the EmailMessageSender class for a mock or fake implementation, but this would risk the implementation in that class.

Another option is to create an end-to-end test for this functionality:

public class EndToEndTest { 
 
  @Test 
  public void email_an_employee() { 
    final StringBuilder systemOutput = 
       injectSystemOutput(); 
    final Employee john = new Employee( 
       new Email("[email protected]")); 
 
    new BirthdayGreetingService().greet(john); 
 
    assertThat(systemOutput.toString(),  
      equalTo("Sent email to " 
        + "'[email protected]' with " 
        + "the body 'Greetings on your " 
        + "birthday'
")); 
  } 
 
  // This code has been used with permission from 
  //GMaur's LegacyUtils: 
  // https://github.com/GMaur/legacyutils 
  private StringBuilder injectSystemOutput() { 
    final StringBuilder stringBuilder = 
      new StringBuilder(); 
    final PrintStream outputPrintStream = 
      new PrintStream( 
        new OutputStream() { 
        @Override 
        public void write(final int b) 
          throws IOException { 
          stringBuilder.append((char) b); 
        } 
      }); 
    System.setOut(outputPrintStream); 
    return stringBuilder; 
  } 
} 

This code has been used with permission from https://github.com/GMaur/legacyutils. This library helps you perform the technique of capturing the system out (System.out).

The name of the file does not end in Specification (or Spec), such as TicTacToeSpec, because this is not a specification. It is a test, to ensure the functionality remains constant. The file has been named EndToEndTest because we try to cover as much functionality as possible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.36