I am trying to write JUnit5 tests for Spring Email. When i am running the test , Greenmail server is not receiving any messages. Attaching code snippets for the same:
@ExtendWith(MockitoExtension.class)
public class EmailServiceImplTest {
Mail emailEntity;
GreenMail serverA;
@InjectMocks
private EmailServiceImpl emailService;
@Mock
private JavaMailSender javaMailSender;
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
emailEntity = mockItemMasterData();
// Setup fake smtp server.
serverA = new GreenMail(ServerSetupTest.SMTP);
serverA.start();
}
private Mail mockItemMasterData() {
Mail mail = new Mail();
mail.setFrom("[email protected]");
mail.setTo("[email protected]");
mail.setSubject("Spring Mail Integration Testing with JUnit and GreenMail Example");
mail.setBody("We show how to write Integration Tests using Spring and GreenMail.");
mail.setSendDate(Date.from(Instant.ofEpochSecond(System.currentTimeMillis())));
return mail;
}
@Test
public void testSendEmailToAvailableMailboxReturnEmail() throws SendFailedException {
// Using javamailsenderimpl to send the mail.
emailService.sendSimpleMessage(emailEntity);
try {
Message[] messages = serverA.getReceivedMessages();
Assert.assertNotNull(messages);
Assert.assertEquals(1, messages.length);
Assert.assertEquals(emailEntity.getSubject(), messages[0].getSubject());
Assert.assertEquals(emailEntity.getBody(), String.valueOf(messages[0].getContent()).contains("body"));
} catch (MessagingException | IOException e) {
Assert.fail("Should be able to retrive sent mail.");
}
}
}
And Following is the class for which i am writing this test
@Component
@Slf4j
public class EmailServiceImpl implements EmailService {
@Autowired
private JavaMailSender javaMailSender;
@Override
public void sendSimpleMessage(Mail mail) throws SendFailedException {
log.info("Send mail started ");
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(mail.getTo());
message.setFrom(mail.getFrom());
message.setSubject(mail.getSubject());
message.setText(mail.getBody());
message.setSentDate(mail.getSendDate());
javaMailSender.send(message);
log.info("Send mail end ");
}
}
Following is the test yaml source:
---
spring:
mail:
default-encoding: UTF-8
host: localhost
jndi-name:
username: username
password: secret
port: 2525
properties:
mail:
debug: false
smtp:
debug: false
auth: true
starttls: true
protocol: smtp
test-connection: false
I am able to send email from EmailServiceImpl class but when I am trying to run test , its not getting any mesages.
I tried everything but didn't get any luck. Any help will be really appreciated.
Thanks.
Start the server/test in debug mode and see the connection parameters of
JavaMailSender
(port and host). Accordingly set exactly these parameters in your test yaml file.