I test sending messages using JavaMailSender , which is launched from ExecutorService using submit , everything works fine, but in GreenMail tests GreenMail does not receive messages, because it finishes work too quickly. Before that I used execute+awaitTermination - everything went off with a bang. setServerStartUpTimeout, setServerConnectionTimeout for GreenMail does not help at any time. How can this be solved? Or are there other libraries to simulate an SMTP server ? Here's the code, what's in the sender

 notifications.forEach(notification -> { Supplier<Boolean> sendingTask = () -> sendNotification(notification); CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(sendingTask, executorService); future.whenComplete((result, ex) -> { if (result) { log.info("Successfull"); } if (ex != null) { log.error("Error by sending", ex); } }); }); 

It's in test

 def setup() { ServerSetup serverSetup = ServerSetupTest.SMTP serverSetup.setServerStartupTimeout(50000) serverSetup.setWriteTimeout(50000) serverSetup.setReadTimeout(50000) serverSetup.setConnectionTimeout(50000) testSmtp = new GreenMail(serverSetup) testSmtp.start() javaMailSender = new JavaMailSenderImpl() javaMailSender.setHost("127.0.0.1") javaMailSender.setPort(3025) } 

and in the test method itself def messages = testSmtp.getReceivedMessages() - shows an empty sheet (

    2 answers 2

    since you run test requests asynchronously, the main thread that started the server ends up without waiting for the asynchronous tasks. Waiting for running tasks can be implemented in different ways:

    • for example CountDownLatch
    • or if the test execution time is not important, but there is no time to deal with CountDownLatch, you can call Thread.sleep in a loop and check the completion of tasks by calling CompletableFuture.isDone()

    other alleged (because there is no full test code) problems:

    • JavaMailSenderImpl uses a non-standard SMTP port (3025), which is not configured in serverSetup

      As Ramiz wrote above, you really need to make the fake server wait for messages to arrive. Used the awailitility library integrated with Groovy .