martes, abril 10, 2012

Why does the rain fall from above? Why do fools fall in love? Why do they fall in love? (Why Do Fools Fall In Love - Frankie Lymon)

More often than not our applications need to send emails to users notifying for example that its account has been created, they have purchased an item, or simply password remaining. When you are writing unit tests there is no problem because probably you will be mocking up interface responsible of sending an email. But what's happen with integration tests?

Maybe the logical path to resolve this problem is installing an email server and execute these tests against it. It is not  a bad idea, but note that you will need to configure your environment before executing your tests.  Your tests will depend on external resources, and this is a bad idea for integration tests. Furthermore these integration tests would not be portable against multiple machines if an email server is not installed previously.

To avoid this problem Dumbster comes to save us. Dumbster is a fake smtp server designed for testing applications that send email messages. It is written in Java so you can start and stop it directly from your tests.

Let's see an example, suppose we are developing an electronic shop, and when an order is placed and email to customer should be sent.

In this case we are going to use Spring Framework 3.1 to create our service layer and will also help us in testing.

Because of teaching purpose, I am not using mail templates, or rich mime types.

First class I am going to show you is Order, which as you can imagine represents an order:

Most important method here is toEmail() that returns email body message.

Next class is service responsible of place an order to delivery system:

This service class uses Spring classes to send an email to customer. See that two methods are present, one that sends a simple message, and the other one called placeOrderWithInvoice that sends an email with an attachment, concretely an invoice in jpg format.

And finally Spring context file:

Note that mail configuration is surrounded by a profile. This means that Spring will only create these beans when application is started up in production mode, and in this case production smtp location is set.

And now let's start with testing:

First of all we must create a Spring context file to configure smtp server location.

See that we are importing application-context.xml file but now we are defining  a new beans profile called integration, where we are redefining smtp connection (changing hostname and port) pointing to fake server.

And finally the test itself.

It is important to explain next parts:
  • @ActiveProfiles is an annotation to tell Spring context which environment should be loaded.
  • SimpleSmtpServer is the main class of Dumbster.
  • @Rule is responsible of starting and stopping smtp server for each method execution.
We have created two tests one that sends a plain message (an_email_should_be_sent_to_customer_confirming_purchase()) and the other one that sends a message with an attachment (an_email_with_invoice_should_be_sent_to_special_customer_confirming_purchase()).

The private methods are simply helper classes to create required classes.

Note that Hamcrest matcher bodyEqualTo comes from BodySmtpMessage class developed specifically for this example.

I wish you have found this post useful, and can give you an alternative when you want to write integration tests involving smtp email service.

Keep Learning,

31 comentarios:

bmatthews68 dijo...

You readers might be interested in a Maven plugin that I recently created to launch a Dumbster server. The idea was to support the development of integration tests for web applications that rely need an e-mail server. The URL is:

Unknown dijo...

Thank you very much for your suggestion, now you can choose between running Dumbster server within Maven lifecycle or JUnit.

Unknown dijo...

On serverside forum Cary Clark wrote a review about this post that I found so useful, I leave here so anyone can read too, thanks Cary.

"I created a fake email server based on Dumbster that accepts incoming email and has a simple UI to display and manage them. The number of emails retained in memory is configurable. When I got tired of Dumbster's bugs, I switched the backend to Wiser: I'm not affiliated with either."

Unknown dijo...

Also may be use FakeSMTP for testing

Unknown dijo...

Obat Aborsi Ampuh
Jual Cytotec Asli
Obat Penggugur Kandungan Janin
Jual Obat Aborsi Cytotec Asli Penggugur Kandungan Tuntas
Jual Obat Aborsi Ampuh
Obat Aborsi
Obat Aborsi Garansi Tuntas

alisha5050 dijo...

All the information in this post is excellent and very useful for all of us, so thanks for sharing this kind of brilliant post. Gurgaon Call Girls

alisha5050 dijo...

Glad to see this kind of brilliant and very interesting informative post. Call Girls in Gurgaon

alisha5050 dijo...

This is a brilliant informative post and great work is done in this post. Gurgaon Escort

alisha5050 dijo...

I really enjoy reading this post because there are so many great and very interesting information is mention in this post.
Escorts in Gurgaon
Escorts Service in Gurgaon
Call Girls Service in Gurgaon

angeleena dijo...

Network Architect is a professional who takes care of communication networks, such as local area networks (LANs), wide area networks (WANs), and intranets.

Network Architect

Tuckerjackk dijo...

Shop Online
Banggood Branding
Great information you shared through this blog.Keep it up and best of luck for your future blogs and posts.

Monika Sahay dijo...

We can without a great deal of stretch find your game to complete your puzzle fantasies. We can without a great deal of stretch restrain the cravings of every man of the word. Our Udaipur escorts service demand one and only real thing out of you they need a confidence and affection.
Udaipur Escorts
Goa Escorts
Jaipur Escorts

officecomsetup dijo...

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative.

Okseoabhinav dijo...

Hi…this is Abhinav here, from few months I am visiting and following you. What I really like about you is that your writing style. Please keep making such as information for us.
Top CA firms in India, Top CA in India.

PcWorld247 dijo...

great blog thank you.

mcafee activation

Kelly Cain dijo...

Nice Blog. Thanks for sharing with us. Such amazing information.

Role of Branding in Making a Business Successful


Guest Blogger

Guest Blogging Site

Guest Blogging Website

Guest Posting Site

Ascorts Club dijo...

We have various type of Celebrity Escorts service in Mumbai that available for All over Mumbai at your place. Our Model Escorts are very beautiful and ready at any time for 100% satisfaction to you. Ascorts Club is The Trustable Escorts Agency in Mumbai.

Celebrity Escorts in Mumbai

Schloka Free Classifieds dijo...

If Your Looking For Call Girls and Escorts Services in Your City? Schloka - Free Adult classifieds in India, Browse in our call girl category To Post Ads, Book Escorts and Find Contact No For Hire Call Girls.

Schloka Escorts Volaba
Schloka Escorts Bandra
Schloka Escorts Andheri
Schloka Escorts Panvel
Schloka Escorts Kalyan
Schloka Escorts Kolhapur

Royal Model dijo...

behala call girl||
cheap call girls near me||
best escort service kolkata||
call girl in dumdum||
escorts service kolkata||
call girls online booking||
kolkata call girl com||
call girl number and image in kolkata||
www kolkata call girl com||
salt lake call girl||
kolkata female escort||
cheap call girl kolkata||
kolkata cheap call girls||
kolkata escorts girls||
lady escort service||
sexy girls in kolkata||
kolkata call girl number||
callgirl in kolkata||
bengali call girl in kolkata||
female escorts kolkata||
high profile call girl in kolkata||
kolkata escort agency||
cheap escort in kolkata||
hot call girl in kolkata||
girls escorts||
sexy girl in kolkata||
callgirl in kolkata||
escort service in||
calcutta call girl||
kolkata escort girls||
call girl in park street kolkata||
call girl booking online||
sex parlour in kolkata||
calcutta escorts||
best call girl in kolkata||
escorts services in kolkata||
kolkata high profile call girl||
female escort services||
hot girls of kolkata||
kolkata call girl mobile number and photo||
www kolkata call girl||
fucking place in kolkata||

Royal Model dijo...

escorts girls in kolkata||
best escort in kolkata||
locanto com kolkata||
best escorts in kolkata||
kolkata call girl service||
bengali escort service||
kolkata scott service||
kolkata sexy call girl||
scot service in kolkata||
escort service in calcutta||
kolkata callgirl||
kolkata call girl phone no||
www call girl in kolkata||
kolkata female escort service||
sexy call girl in kolkata||
kolkata model escorts||
sexy escort kolkata||
kolkata girl escort||
call girl in howrah||
escort girl service||
call girl in kolkatta||
kolkata callgirl||
call garl no||
calcutta escort||
online call girl booking||
kolkata hot call girl||
escort agency kolkata||
escorts at kolkata||
escort at kolkata||
bengali call girl kolkata||
kolkata model escorts||
coll girl kolkata||
kolkata call girl picture and mobile number||
sex service kolkata||
kolkata escorts service||
call girl howrah||
cheap escort services||
call girl in kolkatta||
callgirl kolkata||
sexy call girl kolkata||
call girl of kolkata||
call girl rate in kolkata||
kolkata vip escorts||

Rupali sen dijo...

Get the best Noida Escort service by independent Noida escorts or Noida call girls. Book call girls in Noida or escorts in Noida online visit

Get the best Delhi Escort service by independent Delhi escorts or Delhi call girls. Book call girls in Delhi or escorts in Delhi online visit

JOE MILLER dijo...

Being a software engineer and SEO Expert currently living in the United States. My interests range from writing technology blogs to entrepreneurship. I am also interested in web development and programming.

ankit singh dijo...

MMA training aims to make you learn how to defeat your opponent using techniques of throwing, striking and grappling. MMA classes in Dehradun | best martial arts training in dehradun

VISIT US dijo...

I haven't checked in here for some time since I thought it was getting boring, but the last several posts are good quality so I guess I'll add you back to my everyday blog list. You deserve it my friend 🙂

카지노사이트 dijo...

Well I definitely enjoyed studying it. This information provided by you is very practical for good planning. 포커게임 dijo...

Thank you for providing a good quality article 토토사이트

roulettesitetop dijo...

Hi! Please know how much I appreciate your site and how much I look forward to the new content you provide. 룰렛사이트탑

naina k dijo...

thanks sharing valuable information..

types of laser hair removal

Anónimo dijo...

We take pride in having onboard the most qualified and experienced domain experts with us. We have been successfully offering excellent Online programming assignment help services to students securing them only the best academic grades.

Angel17 dijo...

It's a nice feeling to be in love. Thanks for sharing! property management Siesta Key, FL

Ready dijo...

Great Post. I am Actually Thankful To The Owner Of This Website, Who Has Shared This Impressive Article, Thanks

Noida Female Companion
Varanasi Female Companion
Thane Female Companion