bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)
http://www.simplejavamail.org
Apache License 2.0
1.22k stars 264 forks source link

Would be nice to add mock junit testing support #5

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 9 years ago
It would be nice to add a feature that performs all sorts of validations on the 
email, but stops short from actually delivering the email over the network.  
For unit testing purposes, etc.

Original issue reported on code.google.com by trentj...@gmail.com on 12 Jul 2012 at 1:50

GoogleCodeExporter commented 9 years ago
Any suggestions? Adding a flag 'test mode' is easy enough so that it won't 
actually send emails.

How would you like to perform the validations?

Original comment by b.bottema on 1 Aug 2012 at 10:38

GoogleCodeExporter commented 9 years ago
Consider this:

    // SmtpReceiver is a simple interface definition from simple-java-mail.
    // The interface looks similar to interface used to talk to the remove server, but perhaps at a higher level of abstraction.
    // In another words, I would like to see SmtpReceiver get the "to", "from", "headers", etc., but not actually see the "helo" handshaking, etc.
    // I'd also like to make sure that simple-java-mail still performs all of the other stuff it does like validation.
    // The SmtpReceiver is the last part of the chain.
    // Oh, I'd also like to be able to have SmtpReceiver throw IOExceptions to similate network/server failures, etc.
    SmtpReceiver aSmtpFakeReceiver = <my mock or fake smtpreceiver>

    Mailer mailer = new Mailer(... aSmtpFakeReceiver ...);

Thoughts?

Original comment by trentj...@gmail.com on 1 Aug 2012 at 12:06

bbottema commented 9 years ago

I've given this some thought. I think it would be a good addition as it allows me to test my own code better as well. I would probably not go with an interface, but a concrete class which allows you to override the default behavior.

Thoughts?

musikele commented 8 years ago

Hi @bbottema , in my unit tests I have succesfully used Wiser (https://github.com/voodoodyne/subethasmtp/blob/master/Wiser.md) and Wiser Assertions (https://github.com/kemitix/wiser-assertions) to test send. Maybe it could be of any help to you.

For example:

    @Before
    public void setup() {
        wiser = new Wiser();
        wiser.setPort(2500);
        wiser.start();
    }

    @Test
    public void shouldSendEmail() {
        ...
        WiserAssertions.assertReceivedMessage(wiser)
                .from("santa@claus.com")
                .to("obama@whitehouse.us")
                .withSubject("test mail")
                .withContentContains("duke nukem");
    }

this is my first piece of "markdown" so be indulgent .

bbottema commented 8 years ago

Your markdown is fine :) I edited so that it has java syntax highlighting now and you can see how easy that is.

Regarding your comment on Wiser, that's nice actually. I didn't know of it before. I will let this sink in for some time, because I can't tell if this solution is enough and if the dependency is one I'm comfortable enough with for testing emails going through Simple Java Mail.

bbottema commented 8 years ago

@musikele

I've given this a lot of thought and I'm going to decline this feature, as mocking this library is not the right layer to mock.

Simple Java Mail is a simple pass-through solution translating high abstractions (Email instances) to low level Java Mail SMTP objects (MimeMessages etc.). You should test if your Email objects are being populated properly rather than if the messages being sent on low-level are populated properly. In the latter case you would simply be testing either Simple Java Mail itself or Oracle's SMTP implementation.

Simply mock a Mailer instance directly using something like EasyMock or some other library and test if mailer.sendMail(Email) is being invoked with the expected Email object.

For internal testing of Simple Java Mail, however, I might use WiserAssertions as suggested, but that is to test if Simple Java Mail works as expected and so it is unrelated to this issue here.

bbottema commented 7 years ago

Simple Java Mail now uses Wiser internally to test mail sending correctness!