How can it test email of junit?
Solution use dumbster as a fake smtp server
1. add dependency to pom.xml
dumbster
dumbster
1.6
test
2. add bean definition to spring applicationContext.xml
<constructor-arg index="0" value="2525"/>
using javax mail session
127.0.0.1
2525
<constructor-arg ref="mailSession" type="javax.mail.Session"/>
or if you using spring mail sender:
<bean class="org.springframework.mail.javamail.JavaMailSenderImpl" id="mailSender" p:host="localhost" p:port="2525"/>
3. define one util class
import com.dumbster.smtp.SimpleSmtpServer;
import com.dumbster.smtp.SmtpMessage;
import com.dumbster.smtp.SmtpResponse;
import com.dumbster.smtp.SmtpState;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import java.util.Iterator;
public class BodySmtpMessage extends TypeSafeMatcher<Smtpmessage> {
private SmtpMessage smtpMessage;
public BodySmtpMessage(SmtpMessage smtpMessage) {
this.smtpMessage = smtpMessage;
}
@Override
public boolean matchesSafely(SmtpMessage smtpMessage) {
return smtpMessage.getBody().equals(smtpMessage.getBody());
}
@Override
public void describeTo(Description description) {
description.appendText("not same body message");
}
@Factory
public static <T> Matcher<smtpmessage> bodyEqualTo(SmtpMessage smtpMessage) {
return new BodySmtpMessage(smtpMessage);
}
public static SmtpMessage getExpectedSmtpMessage(String body) {
SmtpResponse smtpResponse = new SmtpResponse(1, "", SmtpState.DATA_BODY);
SmtpMessage expectedMessage = new SmtpMessage();
expectedMessage.store(smtpResponse, body);
return expectedMessage;
}
public static SmtpMessage getFirstReceivedMessage(SimpleSmtpServer simpleSmtpServer) {
Iterator<smtpmessage> receivedEmail = simpleSmtpServer.getReceivedEmail();
while (receivedEmail.hasNext()) {
return receivedEmail.next();
}
return null;
}
}
4. Junit
import com.dumbster.smtp.SimpleSmtpServer;
import com.dumbster.smtp.SimpleSmtpServer;
import com.dumbster.smtp.SmtpMessage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static au.com.hothouse.toyota.utils.email.BodySmtpMessage.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:applicationContext-test.xml")
public class TestEmailerImpl {
@Resource(name = "saveYourCarEmailer")
private EmailerImpl emailer;
@Resource(name = "fakeSmtpServer")
private SimpleSmtpServer simpleSmtpServer;
private static String EXPECTED = "Dear Jianjun Shou,\n" +
"\n" +
"Here is the permanent link to the vehicle you have created:\n" +
"\n" +
"http://test.email.hothouse.com.au\n" +
"\n" +
"Thank you for vising the Toyota website.\n" +
"\n" +
"Regards,\n" +
"\n" +
"The Toyota Team";
@Test
public void testEmail() throws Exception {
emailer.email("Jianjun", "Shou", "sean.shou@hothouse.com.au", "http://test.email.hothouse.com.au");
assertTrue("test failed", simpleSmtpServer.getReceivedEmailSize() > 0);
SmtpMessage sentMessage = getFirstReceivedMessage(simpleSmtpServer);
SmtpMessage expectedSmtpMessage = getExpectedSmtpMessage(EXPECTED);
assertThat(sentMessage, is(bodyEqualTo(expectedSmtpMessage)));
}
}
No comments:
Post a Comment