Skip to content

Instantly share code, notes, and snippets.

@searls
Created March 10, 2011 14:57
Show Gist options
  • Save searls/864209 to your computer and use it in GitHub Desktop.
Save searls/864209 to your computer and use it in GitHub Desktop.
An example of behavior verification with Mockito (on TestNG)
package com.pillartechnology.mail;
public interface ExternalMailSystem {
public void send(String domain, String user, String body);
public void send(Email email);
}
package com.pillartechnology.mail;
public class MailDeliverer {
private ExternalMailSystem externalMailSystem;
public void deliver(String expectedAddress, String expectedBody) {
String[] addressComponents = expectedAddress.split("@");
externalMailSystem.send(addressComponents[1],addressComponents[0], expectedBody);
}
}
package com.pillartechnology.mail;
import static org.mockito.Mockito.*;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class MailDelivererTest {
@InjectMocks private MailDeliverer subject = new MailDeliverer();
@Mock private ExternalMailSystem externalMailSystem;
@BeforeMethod(alwaysRun=true)
public void injectDoubles() {
MockitoAnnotations.initMocks(this);
}
@Test
public void sendsEmail() {
String expectedBody = "Hi Tim!";
subject.deliver("[email protected]",expectedBody);
verify(externalMailSystem).send("wingfield.com","tim", expectedBody);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment