Be able to test printCurrentDate function without changing the method signature. 1.- Test the code with doubles created by you. 2.- Test the code with doubles from a library.
public void printCurrentDate() {
System.out.println(new Date());
}
How to build a Mock and Stub manually.
How to use Mockito to generate the doubles.
@Test
public void should_interact_with_the_mock() {
Collaborator collaborator = mock(Collaborator.class);
MyClass myClass = new MyClass(collaborator);
myClass.run();
verify(collaborator).collaborate();
}
@Test
public void should_retrieve_the_stub_response(){
Collaborator collaborator = mock(Collaborator.class);
String collaboratorResponse = "collaborator response";
when(collaborator.collaborate()).thenReturn(collaboratorResponse);
MyClass myClass = new MyClass(collaborator);
String result = myClass.run();
assertEquals(result, collaboratorResponse);
}