SendMail, Velocity and Spring MVC Contact Form JUnit Highlights

We added JUnit Tests for our recent Spring Mail, Velocity Templates and MVC Contact Form, so in this post we'll cover the more interesting highlights.

SendMail Test

In our Mail Module we want to confirm that our sendContactMail() method creates a MimeMessage by way of a MimeMessagePreparator Callback Interface (2). We don't want to be firing off an email every time (or ANY time) we run our test, so we're going to mock our Mail application's MailSender class and use it when creating our Spring MailService component (1).

Velocity Template Test

Next we want to make sure our Velocity Templates are being populated correctly. We'll take a Spring Environment Property included in our Contact Email Velocity Template and make sure we can find the merged property value in our Velocity output.

MVC Contact Form Tests

We now move from our Mail project to our MVC project where we're going to make sure a malformed email address gets kicked back and that a properly structured Contact Form returns to the page successfully with the friendly “Thanks for the Email!” flash message.

In our bad@emailaddress test we make sure that the from field of our mailDTO object produces an error and that we return to the Contact Form.

We already tested our sendContactEmail() method, so when our {post} action returns our friendly flash message we know we're golden.

Again, we don't want to send off actual emails when testing our controller, so we'll mock our MailService component and use that mock when creating an instance of our MailController.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.

Posted May 05, 2016 12:04 PM EDT

More Like This Post