tags:

views:

210

answers:

4

I'm developing in Java and using JUnit to test some of my methods. Some of my methods send emails. The emails are actually sent by a separate thread that is spawned. The problem is that whenever I test these methods everything goes fine except that the emails are not actually sent. I've followed the execution through the whole stack and every method related to sending the emails is being called and executed correctly. So I tested the methods outside of JUnit and the emails send fine.

+2  A: 

Is the email thread a background (daemon) thread? Perhaps that thread is terminating due to your tests finishing before the emails are sent?

If you put some logging in directly before and after the email is meant to be sent, do those log entries appear?

Jon Skeet
+1  A: 

Do you have anti-virus turned on? I've wasted a couple of hours troubleshooting a similar problem with a silent anti-virus in the background.

llamaoo7
+1 for the nice idea!
furtelwart
I had such a problem once because the port for SMTP was blocked by default for everything but the email client
Mario Ortegón
+2  A: 

If it is a background thread, it is probably being killed silently when the test method finishes. This won't show in the debugger, because you are stepping through.

You might need to write some convenience method in your JUnit test harness that waits at the end of the test method until the mail is actually send. For a quick test, you might put a Thread.sleep() at the end of your test method to see if this helps.

Mario Ortegón
If this is the cause, the test is working—that is, it found a bug in the code that is sending an email. It may be that is code that should be altered to provide the means to check for pending work in background threads.
erickson
A: 

I appreciate all the helpful responses. I tried most of those things. Turns out it just started working. Maybe it was just something with our stack or email server. Kinda lame.

Ben Matz