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,

12 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