Created
March 10, 2011 15:34
-
-
Save searls/864276 to your computer and use it in GitHub Desktop.
An example of stub, verify, and captor with Mockito (on TestNG)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pillartechnology.mail; | |
import java.util.Date; | |
public class Email { | |
private String domain; | |
private String user; | |
private String body; | |
private Date timestamp; | |
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; | |
} | |
public Date getTimestamp() { | |
return timestamp; | |
} | |
public void setTimestamp(Date timestamp) { | |
this.timestamp = timestamp; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pillartechnology.mail; | |
public interface ExternalMailSystem { | |
public void send(String domain, String user, String body); | |
public void send(Email email); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pillartechnology.mail; | |
public class MailDeliverer { | |
private ExternalMailSystem externalMailSystem; | |
private Timestamper timestamper; | |
public void deliver(String address, String body) { | |
Email email = new Email(); | |
email.setBody(body); | |
applyUserAndDomainUsingProvidedAddress(address, email); | |
email.setTimestamp(timestamper.stamp()); | |
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]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pillartechnology.mail; | |
import static org.hamcrest.MatcherAssert.*; | |
import static org.hamcrest.Matchers.*; | |
import static org.mockito.Mockito.*; | |
import java.util.Date; | |
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; | |
@Mock private Timestamper timestamper; | |
@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 | |
public void setsTheTimestampOnTheEmail() { | |
Date expected = new Date(8932l); | |
when(timestamper.stamp()).thenReturn(expected); | |
subject.deliver("a@b", null); | |
verify(externalMailSystem).send(emailCaptor.capture()); | |
Email email = emailCaptor.getValue(); | |
assertThat(email.getTimestamp(),is(expected)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pillartechnology.mail; | |
import java.util.Date; | |
public class Timestamper { | |
public Date stamp() { | |
return null; //TODO - not yet implemented | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment