title: Testing Spring's @Async void-returning methods
body: <p>I have a little problem with <code>@Async</code> methods that return <code>void</code> (or <code>Unit</code>, I'm writing in Kotlin) in my Spring app.</p>
  
  <p>I don't know why, but when <code>@Async</code> method returns <code>void</code> it just doesn't execute, or at least it doesn't do what it is supposed to. Need to say, that in my async methods, I want to send an email using the <code>JavaMailSender</code>, so no big deal. Here is the method:</p>
  
  <pre><code>@Async
  override fun sendEmail(locale: Locale, subject: String, message: String, to: String) {
  
      val msg = sender.createMimeMessage()
      println("1")
      val helper = MimeMessageHelper(msg, true, "utf-8")
      helper.setFrom("email@example.com")
      helper.setTo(to)
      println("2")
      helper.setSubject(getSubject(locale, null))
      println("3")
      helper.setText(processTemplate(locale, null, null), true)
      println("4")
      sender.send(msg)
      println("5")
  }
  </code></pre>
  
  <p>But an email never comes, there is no exception logged (I am running a testNG test).</p>
  
  <p>When I change the signature of the function to make it return <code>Future&lt;String&gt;</code> and add some dummy return line to the end of the function and then I call <code>service.sendEmail(...).get()</code>, the method's body magically executes and the email arrives.</p>
  
  <p>In my <code>@Configuration</code> class, there is <code>@EnableAsync</code>. I also implemented <code>AsyncConfigurer</code> and provided own executor and exception handler, because I thought it could have been something with my executor bean definition, but nothing helps.</p>
  
  <p>This is driving me crazy, because I just want to silently execute something in the background and it doesn't work. By silently I mean that I don't want to be bothered by exceptions thrown inside.</p>
  
  <p>Do you have any ideas?</p>
  
  <p><strong>Update:</strong>
  So as @pleft advised, I put some prints inside my method.
  Now, when I run <code>mvn clean test</code>, I can see that 1,2,3 are printed, <strong>but</strong> not each time. Sometimes just 1 and 2 got printed. I also put print in my <code>AsyncUncaughtExceptionHandler</code>, but that one is not called.
  It looks like the background thread is being killed too soon or something.</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>My ServiceConfig:</p>
  
  <pre><code>@Configuration
  @EnableScheduling
  @EnableAsync
  @ComponentScan
  class ServiceConfig : ApplicationContextAware, EnvironmentAware, AsyncConfigurer {
      override fun getAsyncUncaughtExceptionHandler(): AsyncUncaughtExceptionHandler {
          return AsyncUncaughtExceptionHandler { ex, method, params -&gt;
              println("Exception thrown")
          }
      }
  
      override fun getAsyncExecutor(): Executor {
          val executor = ThreadPoolTaskExecutor()
          executor.corePoolSize = 2
          executor.maxPoolSize = 2
          executor.setQueueCapacity(500)
          executor.threadNamePrefix = "asyncThread"
          executor.initialize()
          return executor
      }
  }
  /// other beans definitions..
  </code></pre>
  
  <p>Maybe it is important, maybe not, but I'm using Thymeleaf in that <code>processTemplate</code> method.</p>