Skip to content

Instantly share code, notes, and snippets.

@efleming969
Last active October 20, 2016 18:33
Show Gist options
  • Save efleming969/969fc36a64bce5819e2984854226ff05 to your computer and use it in GitHub Desktop.
Save efleming969/969fc36a64bce5819e2984854226ff05 to your computer and use it in GitHub Desktop.
package example;
import java.util.*;
import java.io.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.*;
public class MockingExample {
@Test
public void should_build_a_greeting_based_on_age() {
DataService ds = mock( DataService.class );
when( ds.getMessageBasedOnAge( 45 ) ).thenReturn( "Whazzup" );
Greeting g = new Greeting( ds );
String message = g.build( "Joe", 45 );
assertEquals( "Whazzup, Joe!", message );
}
@Test
public void should_send_an_email_message() {
EmailService es = mock( EmailService.class );
Greeting g = new Greeting( es );
g.sayHello( "Joe" );
verify( es ).send( "Whazzup, Joe!" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment