0

I'm writing a test like this

public String cc = "";

@Test
public void testSendMailWithMissingData() throws MessagingException {

    String sender = "test@test.com";
    String receiver = "test2@test.com";
    String subject = "";
    String content = "";

    javaMailSenderImpl.setPort(greenMail.getSmtp().getPort());
    iMailService.sendMail(sender, receiver, subject, content, cc);

    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(subject, emails[0].getSubject());
    ......
    }

@Test
public void testSendMailWIthData() throws MessagingException {

    String sender = "test@test.com";
    String receiver = "receiver@receiver.com";
    String subject = "test_subject";
    String content = "test_content";
    cc = "ccaddress@cc.com";

    javaMailSenderImpl.setPort(greenMail.getSmtp().getPort());
    iMailService.sendMail(sender, receiver, subject, content, cc);

    MimeMessage[] emails2 = greenMail.getReceivedMessages();
    assertEquals(cc, InternetAddress.toString(emails2[0].getRecipients(Message.RecipientType.CC)));

     }

but I get in testSendMailWIthData that exspected cc is ccaddress@cc.com but is null. Why? Is it because I use same port? Why I cant use two different String values in two different @Test?

Help

JUNIT log:

java.lang.AssertionError: expected:<ccaddress@cc.com> but was:<null>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:743)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)
    ...
lpkej
  • 445
  • 6
  • 23
  • 1
    `InternetAddress.toString(emails2[0].getRecipients(Message.RecipientType.CC))` returned null. First argument is expected, second is actual - refer to [Junit docs](http://junit.sourceforge.net/junit3.8.1/javadoc/junit/framework/Assert.html) – Michał Krzywański Jul 30 '19 at 15:02
  • 1
    In `testSendMailWIthData()` can you retrieve all the fields of each email in `emails2[]`? I think you are receiving the email sent from the first test as well. In the first test you have sent an email with CC null. That could be the reason. I recommend you to print all email fields like sender, receiver, subject, content, and cc. If that's the case, to avoid the issue either use different server/port in each test OR while asserting identify the correct email first (say by sender and keep sender email address different in each test) and then assert remaining fields. – fiveelements Jul 30 '19 at 15:17

2 Answers2

2

In testSendMailWIthData() can you retrieve all the fields of each email in emails2[]?

I think you are receiving the email sent from the first test as well in the second test. In the first test, you have sent an email with CC null/absent. That could be the reason you are getting null.

I recommend you to print all email fields like sender, receiver, subject, content, and cc to confirm this. If that's the case, to avoid the issue either use different server/port in each test OR while asserting identify the correct email first (say by sender and keep sender email address different in each test) and then assert remaining fields.

Another option could be - first delete all emails from a specific sender (test@test.com) as a pre-step (@Before) in each test and then proceed with send, retrieve and assert.

fiveelements
  • 3,649
  • 1
  • 17
  • 16
1

In your

assertEquals(cc, InternetAddress.toString(emails2[0].getRecipients(Message.RecipientType.CC)));

cc is "ccaddress@cc.com"

JUnit log said to you about it for this assertEquals. Look at the method signature:

public static void assertEquals(double expected, double actual)

You may test it with:

assertEquals("ccaddress@cc.com", cc)
Anatoly
  • 54
  • 4
  • The thing is I sent message successfully, but cc value in embedded greenmail email is null. Same I get expected "ccaddress@cc.com" but got null. Can it be that I use same email[0] ? I sent email in first Test and actually he sees that cc is null and in the second Test he sees the same null cc value from the same ```email```? – lpkej Jul 30 '19 at 15:16
  • 2
    I do not think the question is about the order of assertion. His main ask is what is the reason of getting `null` value. – fiveelements Jul 30 '19 at 15:20
  • 1. JUnit may test metods in different order. 2. It may take a time for delivering message. (sent does not mean delivered) – Anatoly Jul 30 '19 at 15:23
  • I've tested, there is email in mailbox, but with different values – lpkej Jul 30 '19 at 15:31
  • 1
    Generally relying on mutable state in different tests is bad. Create those Strings as local test variables and keep only final static values at class level (or values that are set by @Before set up method) – Michał Krzywański Jul 30 '19 at 15:35
  • What about a pause between sendMail and getReceivedMessages, check how many messages have a service and find yours. – Anatoly Jul 30 '19 at 15:41