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,
Alex.

31 comentarios:

  1. 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:
    http://emailserver-maven-plugin.btmatthews.com/

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

    ResponderEliminar
  3. 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: http://code.google.com/p/subethasmtp/wiki/Wiser. I'm not affiliated with either."

    ResponderEliminar
  4. 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

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

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

    ResponderEliminar
  7. 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

    ResponderEliminar


  8. 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

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

    ResponderEliminar
  10. 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

    ResponderEliminar
  11. 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.

    office.com/setup
    norton.com/setup
    mcafee.com/activate
    mcafee.com/activate
    webroot.com/safe

    ResponderEliminar
  12. 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.

    ResponderEliminar
  13. 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

    ResponderEliminar
  14. 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

    ResponderEliminar
  15. 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 https://www.pyaliescortservice.com/

    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 https://www.nancymittal.com/

    ResponderEliminar
  16. 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.
    www.office.com/setup
    www.office.com/setup
    www.office.com/setup
    www.office.com/setup

    ResponderEliminar
  17. 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

    ResponderEliminar
  18. 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 🙂

    야설
    오피
    외국인출장
    마사지
    카지노사이트

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

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

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

    ResponderEliminar
  22. Anónimo12:37 p. m.

    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.

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

    ResponderEliminar
  24. 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

    ResponderEliminar