Created
March 10, 2011 14:57
-
-
Save searls/864209 to your computer and use it in GitHub Desktop.
An example of behavior verification 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; | |
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; | |
public void deliver(String expectedAddress, String expectedBody) { | |
String[] addressComponents = expectedAddress.split("@"); | |
externalMailSystem.send(addressComponents[1],addressComponents[0], expectedBody); | |
} | |
} |
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.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