Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly test @Async mailers in Spring 3.0.x?
    primarykey
    data
    text
    <p>I have several mailers that I'd just love to slap @Async on and reap the benefits of asynchronous mailing.</p> <p>The only problem I'm having is that I don't know how to properly test them, and I'd like to easily take the method I'm currently testing them with and just modify it without making massive changes to the test code if that's possible.</p> <p>For example, in a test class, I'll define two autowired beans. One is the mailer service, which is responsible for doing all mail-like transactions, and the other is the JavaMailSender - but it's a mock. I then place the mock into the service so that it doesn't actually send the real emails ;)</p> <pre><code>@Autowired Mailer mailer; MockJavaMailSender mailSender; @Before public void setup() { mailSender = new MockJavaMailSender(); mailer.setMailSender(mailSender); } </code></pre> <p>This approach has worked out really well, because I can just ask my mock questions or get data from it to make sure my mailer code works:</p> <pre><code>UserAccount userAccount = userAccountDao.find(1); mailer.sendRetrievePassword(userAccount); mailSender.assertTimesSent(1); String text = mailSender.getMimeMessage().buildText(); // Do tests on text. </code></pre> <p>The problem with @Async is that mailSender won't be populated yet, so the tests will fail.</p> <p>here is the code that uses @Async:</p> <pre><code>@Async @Override public void sendRetrievePassword(UserAccount userAccount) { mailSender.send(new MimeMessageSupport(velocityEngine) .setTitle("Retrieve Password") .setTemplate("mail/retrievePassword.vm") .setToEmailAddress(userAccount.getEmailAddress()) .addObject("userAccount", userAccount)); } </code></pre> <p>Is there a really easy way to correct this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload