Skip to content

Instantly share code, notes, and snippets.

@vgallet
Created April 29, 2018 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgallet/fc43785a96d7613cab3f48f30ec8f4b3 to your computer and use it in GitHub Desktop.
Save vgallet/fc43785a96d7613cab3f48f30ec8f4b3 to your computer and use it in GitHub Desktop.
public class SMTPServerExtension implements BeforeEachCallback, AfterEachCallback {
private GreenMail smtpServer;
private String hostname;
private int port;
public SMTPServerExtension() {
this(25);
}
public SMTPServerExtension(int port) {
this("localhost", port);
}
public SMTPServerExtension(String hostname, int port) {
this.hostname = hostname;
this.port = port;
}
public List<ExpectedMail> getMessages() {
return Lists.newArrayList(smtpServer.getReceivedMessages()).stream()
.parallel()
.map(mimeMessage -> ExpectedMail.transformMimeMessage(mimeMessage)).collect(Collectors.toList());
}
@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
smtpServer.stop();
}
@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
smtpServer = new GreenMail(new ServerSetup(port, hostname, "smtp"));
smtpServer.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment