Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
An example of behavior verification + argument capture with Mockito (on TestNG)
package com.pillartechnology.mail;
import java.util.Date;
public class Email {
private String domain;
private String user;
private String body;
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
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 address, String body) {
Email email = new Email();
email.setBody(body);
applyUserAndDomainUsingProvidedAddress(address, email);
externalMailSystem.send(email);
}
private void applyUserAndDomainUsingProvidedAddress(String address, Email email) {
//TODO - refactor this into the Email class (constructor? smart accessor?)
String[] addressComponents = address.split("@");
email.setUser(addressComponents[0]);
email.setDomain(addressComponents[1]);
}
}
package com.pillartechnology.mail;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
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;
@Captor private ArgumentCaptor<Email> emailCaptor;
@BeforeMethod(alwaysRun=true)
public void injectDoubles() {
MockitoAnnotations.initMocks(this);
}
@Test
public void sendsEmailByConstructingEmailObject() {
String expectedUser = "tim";
String expectedDomain = "wingfield.com";
String expectedBody = "Hi Tim!";
subject.deliver(expectedUser+"@"+expectedDomain,expectedBody);
verify(externalMailSystem).send(emailCaptor.capture());
Email email = emailCaptor.getValue();
assertThat(email.getUser(),is(expectedUser));
assertThat(email.getDomain(),is(expectedDomain));
assertThat(email.getBody(),is(expectedBody));
}
@Test(enabled=false) //"WIP - We're converting to the service method that sends an Email object"
public void sendsEmailBySplittingAddress() {
String expectedBody = "Hi Tim!";
subject.deliver("tim@wingfield.com",expectedBody);
verify(externalMailSystem).send("wingfield.com","tim", expectedBody);
}
}

Thank you so much for publishing this, I was having a hard time following the api docs, but this explained it very clearly.

Thank you so much.. Surely it will help me.. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment