Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the ability to instantiate GreenMail per-class in addition to per-method #332

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.icegreen.greenmail.util.ServerSetup;
import com.icegreen.greenmail.util.ServerSetupTest;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -18,9 +20,10 @@
* @see RegisterExtension
* @see TestInstance
*/
public class GreenMailExtension extends GreenMailProxy implements BeforeEachCallback, AfterEachCallback {
public class GreenMailExtension extends GreenMailProxy implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
private final ServerSetup[] serverSetups;
private GreenMail greenMail;
private boolean perMethod = true;

/**
* Initialize with single server setups.
Expand Down Expand Up @@ -49,13 +52,46 @@ public GreenMailExtension(final ServerSetup[] serverSetups) {

@Override
public void beforeEach(final ExtensionContext context) {
greenMail = new GreenMail(serverSetups);
start();
if (perMethod) {
greenMail = new GreenMail(serverSetups);
start();
}
}

@Override
public void afterEach(final ExtensionContext context) {
stop();
if (perMethod) {
stop();
}
}

@Override
public void beforeAll(ExtensionContext context) throws Exception {
if (!perMethod) {
greenMail = new GreenMail(serverSetups);
start();
}
}

@Override
public void afterAll(ExtensionContext context) throws Exception {
if (!perMethod) {
stop();
}
}

/**
* Specify whether GreenMail should be set up and torn down before and after
* each method or before and after all methods.
*
* @param perMethod
* If <code>true</code>, per-method lifecycle is used, per-class
* otherwise.
* @return The GreenMail extension for chaining calls.
*/
public GreenMailExtension withPerMethodLifecycle(boolean perMethod) {
this.perMethod = perMethod;
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.icegreen.greenmail.junit5;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import com.icegreen.greenmail.configuration.GreenMailConfiguration;
import com.icegreen.greenmail.util.GreenMailUtil;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("GreenMail with alternate lifecycles tests")
class AlternateLifecyclesTests {

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("Per-Method lifecycle")
class PerMethodLifecycle {
@RegisterExtension
GreenMailExtension greenMail = new GreenMailExtension()
.withConfiguration(GreenMailConfiguration.aConfig()
.withUser("[email protected]", "login-id", "password"));

@Test
@DisplayName("Send test 1")
void testSend1() {
GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "some subject", "some body");
assertEquals(1, greenMail.getReceivedMessages().length);
}

@Test
@DisplayName("Send test 2")
void testSend2() {
GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "some subject", "some body");
assertEquals(1, greenMail.getReceivedMessages().length);
}

@AfterAll
public void afterAll() {
assertEquals(0, greenMail.getReceivedMessages().length);
}
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("Per-Class lifecycle")
class PerClassLifecycle {
@RegisterExtension
GreenMailExtension greenMail = new GreenMailExtension()
.withPerMethodLifecycle(false)
.withConfiguration(GreenMailConfiguration.aConfig()
.withUser("[email protected]", "login-id", "password"));

@Test
@DisplayName("Send test 1")
void testSend1() {
GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "some subject", "some body");
}

@Test
@DisplayName("Send test 2")
void testSend2() {
GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", "some subject", "some body");
}

@AfterAll
public void afterAll() {
assertEquals(2, greenMail.getReceivedMessages().length);
}
}
}