title: Junit5 Tests for Spring Email with GreenMail Server using SMTP
body: <p>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: </p>
  
  <pre><code>@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("no-reply@test.com");
      mail.setTo("no-reply@test.com");
      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.");
      }
  }
  
  }
  </code></pre>
  
  <p>And Following is the class for which i am writing this test</p>
  
  <pre><code>@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 ");
   }
   }
  </code></pre>
  
  <p>Following is the test yaml source:</p>
  
  <pre><code>---
  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
  </code></pre>
  
  <p>I am able to send email from <strong>EmailServiceImpl</strong> class but when I am trying to run test , its not getting any mesages.</p>
  
  <p>I tried everything but didn't get any luck.
  Any help will be really appreciated.</p>
  
  <p>Thanks.</p>