Merge pull request #7 from kemitix/issue-6-unexpected-mimemessage-content-in-withcontentcontains

Fix Issue 6 Unexpected mimeMessage content in withContentContains
This commit is contained in:
Paul Campbell 2016-02-16 23:06:35 +00:00
commit 2309ce04f0
4 changed files with 50 additions and 3 deletions

View file

@ -57,12 +57,18 @@
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<dependency><!-- for Issue1Test -->
<groupId>org.codemonkey.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
<dependency><!-- for Issue6Test -->
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View file

@ -181,6 +181,9 @@ public final class WiserAssertions {
private String getMimeMessageBody(final WiserMessage message)
throws IOException, MessagingException {
Object content = getMimeMessage(message).getContent();
if (content instanceof String) {
return (String) content;
}
if (content instanceof MimeMessage) {
return content.toString();
}

View file

@ -56,8 +56,8 @@ public abstract class AbstractWiserTest {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", "localhost");
properties.setProperty("mail.smtp.port", "" + WiserAssertionsTest.PORT);
Session session = Session.getDefaultInstance(properties);
properties.setProperty("mail.smtp.port", "" + PORT);
Session session = Session.getInstance(properties);
return session;
}

View file

@ -0,0 +1,38 @@
package net.kemitix.wiser.assertions;
import org.junit.Test;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
/**
* Regression test for issue #6.
*
* @see https://github.com/kemitix/wiser-assertions/issues/6
* @author pcampbell
*/
public class Issue6Test extends AbstractWiserTest {
/**
* Test {@link WiserAssertions#withContentContains(String)} where the
* message is a Spring Mail {@link SimpleMailMessage}.
*/
@Test
public void shouldMatchContentContainsFromSimpleMailMessage() {
//given
SimpleMailMessage message = new SimpleMailMessage();
message.setTo("Carl <carl@b.com>");
message.setFrom("Bob <bob@a.com>");
message.setSubject("Subject");
message.setText("Hi Carl,\n\nA new message was just posted.");
final JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setPort(PORT);
//when
sender.send(message);
//then
getAssertions().from("bob@a.com").to("carl@b.com")
.withSubject("Subject")
.withContentContains("Hi Carl");
}
}