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,

27 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

Dr. Lina dijo...

Jual obat aborsi cytotec
Jual obat aborsi bandung
Jual obat aborsi batam
Jual obat aborsi makassar

Jual obat aborsi malang
Jual obat aborsi palembang
Jual obat aborsi semarang

Jual obat aborsi jakarta
Jual obat aborsi bekasi

Jual obat aborsi surabaya ,Semarang, Jogja, yogyakarta,Malang, Surabaya, Jakarta, Bandung, Bekasi, Batam, Palembang dan Makassar.

Unknown dijo...شركة-ديكور-بالرياض/تركيب-طارد-الحمام-بالرياض/شركة-دهانات-بالرياض/نقل-اثاث-الرياض-الكويت/نقل-اثاث-من-الرياض-الى-الدمام/

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

Jaipur Escorts Services dijo...

Ajmer Escorts Service

Ajmer Escorts

Ajmer Escorts Service

Escorts In Ajmer

Escorts Ajmer

Call Girls in Rewari

Call Girls In Jaipur

call girls in Udaipur

call girl Udaipur

Udaipur call girl

Udaipur Escorts Service

call girls in jaipur

escorts in jaipur

escort services in jaipur

call girls jaipur

jaipur call girls

Call Girls In Jaipur

call girl service jaipur

Call Girls In Vijaynagar

Udaipur Call Girls

Call Girls In Jaipur

Call Girls in Marathahalli

riya4521 dijo...

Delhi has experienced the origin of housewives offering their services. Housewife Delhi escorts . I am riya here work as a high profile Call girl in delhi . I am here for last 6 years and have ability of fulfilling every sexual desires of every man. I am truly a self-independent and always ready to enjoy with you. refer to the women who have already been married and stay in the house to perform the household chores.
housewives are cunning enough to keep their personal lives hidden under a veil of ignorance and innocence. Noida escorts for you night. then luckily you are landed on the right webpage. I am seema a hot sexy high demanding. my demand is the proof of my popularity among my clients. if you looking for Noida call girls sexy killer queen I am perfect for you. I am professional in husbands believe that their wives have gone to give coaching to infants or children in school. Faridabad escorts This is pretty unfortunate as the truth is never revealed and the men are none the wiser.
gurgaon Call girls
Ghaziabad call girls
Haldwani escorts
Ghaziabad escorts
gurgaon Escorts
escorts in delhi

helen sharma dijo...

Noida Escorts Well even while seated by a laptop screen on your comfortable bed is not going to Faridabad escorts provide you with the comfort you Gurgaon escorts have been looking forward to, so that all your work-related problems ghaziabad escorts right system where you get wonderful, hot and attractive women escorts to satisfy Delhi escorts
all your goals and you can afford getting something pleased or getting yourself encounter pleased be even if it is a smaller part of their lifestyle or the big boring schedule that appears ahead. Nainital escorts
escorts in gurgaon

Mona Singh dijo...

Mona sharma are dating lover we have the best Noida escorts The Noida escort service is going through a wonderful time in its long service to the state. Noida call girls women in the neighborhood now have a better lifestyle than what they had previously.

Independent Noida escorts state some reasons for the change in their lifestyle. delhi escorts One of the primary reasons for this change in the lifestyle is the expansion of the escort industry. Over the last few months delhi call girls They have understood about the measures to be taken to ensure that sex does not bring about a misfortune in their lives.
gurgaon escorts
gurgaon call girls
ghaziabad escorts
ghaziabad call girl
escorts ghaziabad
Faridabad escorts
Faridabad call girls
escorts in noida
Ramnagar escorts

escort datting dijo...

Welcome to Sohna Road Escorts Service. Sohna Road offers you online Kalinga for getting erotic call girls. Call fast at cheap and good rate Escort Service In Gurgaon With Mobile Number

mumbaigirls dijo...

Want to experiment pleasure and excitement in life? We have the perfect answer to your imagination! Enjoy the warm company of Call Girls in Delhi With Photo and take your senses to a new level of pleasure. When it comes to providing best Delhi girls, Mobile no one does it better than us. Delhi Escorts With Photos

escort datting dijo...

Welcome to Sohna Road Escorts Service. Sohna Road offers you online Kalinga for getting erotic call girls. Call fast at cheap and good rate Escort Service In Gurgaon With Mobile Number

Call Girls In Gurgaon With Whatsapp Number dijo...

Welcome to Gurgaon Escorts Agency, we give you complete discount of receiving Russian and erotic call girls in Gurgaon, you can contact us on our website to get them. Call Girls In Gurgaon With Whatsapp Number

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.

maaz dijo...

I appreciate the time you’ve taken out to show me the ropes and ensure that I have the required knowledge to do a great job. Thank you.
read more

Monika Sahay dijo...

It's Been discovered that every area within the state has one or even the contrary specialty that generates the location cost seeing. Aside from the contrary appeals, the escorts in Ajmer are appreciating at a very important role in raising the total amount of holiday makers and Tourists inside town within the prior few years.
Ajmer Escorts
Kishangarh Escorts
Pushkar Escorts
Beawar Escorts
Nasirabad Escorts

Monika Sahay dijo...

The escort service in Nainital, among the most prestigious independent escorts in Nainital, provides one of the very best women escorts in Nainital. Our beautiful call girls in Nainital buddy for people who want assistance for a short time or should appearing spend the entire night in Nainital at 5-star accommodation room.
Nainital Escorts
Ramnagar Escort service
Bhimtal Escort service
Haldwani Escort service
Rudrapur Escort service

Monika Sahay dijo...

Just how it may be carried out? Yes, it is a clear concern for the ones that are new to the place and completed have sufficient concept concerning such Hyderabad escort services. Within this kind of circumstance where an individual has much less comprehension, chances are really greater to get from the grab of an artificial firm.
Hyderabad Escorts
Ahmedabad Escorts
Jodhpur Call Girls
Dehradun Escorts

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

jerome dijo...

We have the most excellent collection of Escort Service in Gurgaon that will impress you with joy and satisfaction with their services. There is a lot of doing which takes pleasure in Call Girls in Gurgaon, a lot of which excludes lovemaking and other smutty activities. However, there is more that you can do to capitalize on your stay at Gurgaon Escorts, but that is only a portion of the proposition.