Created
March 10, 2011 15:12
-
-
Save searls/864224 to your computer and use it in GitHub Desktop.
An example of behavior verification + argument capture with Mockito (on TestNG)
This file contains 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; | |
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; | |
} | |
} |
This file contains 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 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; | |
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]); | |
} | |
} |
This file contains 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 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("[email protected]",expectedBody); | |
verify(externalMailSystem).send("wingfield.com","tim", expectedBody); | |
} | |
} |
Thank you so much.. Surely it will help me.. :)
Great Example !!!!
Nice example
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for publishing this, I was having a hard time following the api docs, but this explained it very clearly.