Created
April 28, 2015 12:39
-
-
Save MaximeFrancoeur/bf01e15f3d5ed76cc7c4 to your computer and use it in GitHub Desktop.
Complex Stubbing with Mockito for Java
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
@Mock private SmtpConnection smtpConnection; | |
private void mockSmtpConnection() { | |
final ArgumentCaptor<String> recipient = ArgumentCaptor.forClass(String.class); | |
final ArgumentCaptor<String> subject = ArgumentCaptor.forClass(String.class); | |
final ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class); | |
when(smtpConnection.setRecipient(recipient.capture())) | |
.thenReturn(smtpConnection); | |
when(smtpConnection.setSubject(subject.capture())) | |
.thenReturn(smtpConnection); | |
when(smtpConnection.setMessage(body.capture())) | |
.thenReturn(smtpConnection); | |
when(smtpConnection.getOutgoingClientData()) | |
.thenAnswer(new Answer<String> { | |
@Override | |
public String answer(InvocationOnMock call) { | |
return recipient.getValue() + "\0" | |
+ subject.getValue() + "\0" | |
+ body.getValue(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment