title: How to test JavaMailSender?
body: <p>I have little problem. I created MailService to send mails. When I run program, it works. All properties to email I have in <code>resources/application.properties</code>. I'm using <code>spring-boot-starter-mail</code>.</p>
  
  <pre><code>@Service
  public class MailService {
      private JavaMailSender javaMailSender;
  
      @Autowired
      public MailService(JavaMailSender javaMailSender) {
          this.javaMailSender = javaMailSender;
      }
  
      public void sendMail(String subject, String messageContent, String recipient)
              throws MessagingException {
          MimeMessage mimeMessage = javaMailSender.createMimeMessage();
          MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
          messageHelper.setTo(recipient);
          messageHelper.setSubject(subject);
          messageHelper.setText(messageContent);
          javaMailSender.send(mimeMessage);
      }
  }
  </code></pre>
  
  <p>But I don't have idea how can I create test for it. I tried something like this, where I use <code>org.jvnet.mock-javamail:mock-javamail</code>, but it doesn't work:</p>
  
  <pre><code>public class MailServiceTest {
      private MailService mailService;
  
      @Mock
      private JavaMailSender javaMailSender;
  
  
      @Before
      public void setUp() {
          MockitoAnnotations.initMocks(this);
          mailService = new MailService(javaMailSender);
          Mailbox.clearAll();
      }
  
      @Test
      public void sendMailTest() throws MessagingException, IOException {
          String subject = "Some subject";
          String body = "Some contents.";
          String recipient = "test@test.com";
  
          mailService.sendMail(subject, body, recipient);
          List&lt;Message&gt; inbox = Mailbox.get(recipient);
          assertTrue(inbox.size() == 1);
          assertEquals(subject, inbox.get(0).getSubject());
          assertEquals(body, inbox.get(0).getContent());
      }
  }
  </code></pre>