martes, octubre 24, 2017

Testing Code that requires a mail server

Almost all applications has one common requirement, they need to send an email notifying something to a registered user. It might be an invoice, a confirmation of an action or a password recovery. How to test this use case might be challenging, using mocks and stubs are ok for unit tests, but having a component test that tests the whole stack. 

In this post I am going to show you how Docker and MailHog, can help you on testing this part of code.

MailHog is  super simple SMTP server for email testing tool for developers:
  • Configure your application to use MailHog for SMTP delivery
  • View messages in the web UI, or retrieve them with the JSON API
  • Optionally release messages to real SMTP servers for delivery
  • Docker image with MailHog installed
Notice that since you can retrieve any message sent to mail server using JSON API, it makes really simple to validate if the message has been really delivered and of course assert on any of message fields.

Arquillian Cube is an Arquillian extension that can be used to manager Docker containers in your tests. To use it you need a Docker daemon running on a computer (it can be local or not), but probably it will be at local.

Arquillian Cube offers three different ways to define container(s):
  • Defining a docker-compose file.
  • Defining a Container Object.
  • Using Container Object DSL.
In this example I am going to show you Container Object DSL approach, but any of the others works as well.

To use Container Object DSL you simply need to instantiate the ContainerDslRule (in case you are using JUnit Rules) or use Arquillian runner in case of using JUnit, TestNG or Spock. You can read more about Container Object DSL at http://arquillian.org/arquillian-cube/#_arquillian_cube_and_container_object_dsl

As an example of definition of Redis container:


When running this test, Redis Docker image is started, tests are executed and finally the Docker instance is stopped.

So let's see how to do the same but instead of Redis using Mailhog Docker image.
It is important to notice that ContainerDslRule is a generic class that  can be extended to become more specific to a concrete use case. And this is what we are going to do for Mailhog.

First of all we need to create a class extending from ContainerDslRule, so everything is still a JUnit rule, but a customized one. Then we create a factory method which creates the MailhogContainer object, setting the image name and the binding ports. Finally an assertion method is used to connect to Rest API of Mailhog server to check if there is any message with given subject.

Then we can write a test using this new rule.


This test just configures MailService class with Docker container configuration, it sends an email and finally it delegates to container object to validate if the email has been received or not.

Notice that putting everything into an object makes this object reusable in other tests and even in other projects. You can create an independent project with all your custom developed container objects and just reuse them importing them as test jar in hosted project.

Code: https://github.com/lordofthejars/mailtest

We keep learning,
Alex.
'Cause I'm kind of like Han Solo always stroking my own wookie, I'm the root of all that's evil yeah but you can call me cookie (Fire Water Burn - Bloodhound Gang)




8 comentarios:

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.

yanmaneee dijo...

balenciaga sneakers
nike air force
adidas nmd
curry 6 shoes
yeezy
ultra boost 3.0
calvin klein outlet
air max 97
ferragamo sale
christian louboutin

yanmaneee dijo...

vapormax
calvin klein outlet
yeezy boost
nike air max 97
lebron shoes
hermes handbags
off white x jordan 1
balenciaga
converse outlet store
kyrie 5 shoes

Anónimo dijo...

🔥 ☑️MEET THE REAL HACKERS☑️ 🔥

It Tears me Up Whenever we receive complaints from People About Their Experience With the Hackers They Met Before They Heard about us.
These Days There Are alot of Hackers Online, You Just Have to Be Careful about who you meet for help, because many people now don't really know who to ask for help anymore but there is an actual solution to that which I am giving you for free, Trust Me You Don't Wanna go out there seeking for Hackers Yourself, Because the probability of getting a Real Hacker Out there Is Very Slim . ❌❌ ❌ Most Of Them are actually not who they say they're, they are just out there to Rip people Off, Trust Me You Don’t Wanna Risk It. You Can Always Identify Them With Their False Advertisements and False Testimonies Trying To Lure you Into their Arms, And most of them use yahoomails, gmails and other cheap email providers which could easily expose their vulnerabilities, Please Don’t Fall For Them🚷 Come To Think Of It, Why would a Legit Hacker Be using a gmail or a cheap email provider that exposes his vulnerabilities????⚠️⚠️⚠️
Well, Our Purpose Here Is To Link You Up With Top Notched Legit Hackers With Great Online Reputations and Impressive LinkedIn Profiles That’ll Blow Your Mind. Trust Me, You Don’t Wanna Miss This Great Opportunity.

🔥 ☑️ COMPOSITE CYBER SECURITY SPECIALISTS are the Answers to your PRAYERS . We are here to Provide you with The BEST and FASTEST Hackers that would get your Job Done as Fast as possible. 🔥 🔥


☑️All our Specialists are well experienced in their various niches with Great Skills, Technical Hacking Strategies And Positive Online Reputations And Recommendations🔘
They hail from a proven track record and have cracked even the toughest of barriers to intrude and capture all relevant data needed by our Clients. 🔥 🔥

We have Digital Forensic Specialists⚡️, Certified Ethical Hackers⚡️, Software Engineers⚡️, Cyber Security Experts⚡️, Private investigators and more⚡️⚡️. Our Goal is to make your digital life secure, safe and hassle free by Linking you Up With these great Professionals such as JACK CABLE, ARNE SWINNEN, SEAN MELIA, DAWID CZAGAN, BEN SADEGHIPOUR And More. These Professionals are Well Reserved Professionals who are always ready to Handle your job with great energy and swift response so that your problems can be solved very quickly.
All You Need to Do is to send us a mail and we’ll Assign any of these specialists to Handle your Job immediately. ⚡️⚡️⚡️

☑️ Below Is A List Of Our Major Services:
▪️ FUNDS RECOVERY ON BITCOIN SCAM, INVESTMENTS, BINARY OPTIONS TRADING and ALL TYPES OF SCAMS.
▪️ WEBSITE AND DATABASE HACKING 💻
▪️ CREDIT REPAIR. 💳
▪️ PHONE HACKING & CLONING (giving you 📱 Unnoticeable access to everything Happening on the Target’s Phone)
▪️ CLEARING OF CRIMINAL RECORDS ❌
▪️ SOCIAL MEDIA ACCOUNTS HACKING 📱
▪️RECOVERY OF DELETED FILES 📤
▪️LOCATION TRACKING 📌
▪️BITCOIN MINING ⛏ And lot More.

✅ COMPOSITE CYBER SECURITY SPECIALISTS are Basically the ANSWERS to your PRAYERS. Trust Me, You Don’t Wanna Miss Out This Great Experience.

📩CONTACT US TODAY:
Email:
composite.cybersecurity@protonmail.com

🔘2020 © composite cybersecurity specialists
🔘Want faster service? Contact us!
🔘All Rights Reserved ®️










































efren dijo...

Seeing is believing they say, I knew something was wrong but I couldn't find my way around it. A friend introduced me WhitehatstechATgmailDOTcom, who without physical access to my spouse's phone got full access to all texts, calls, facebook, whatsapp and other social media. I find out my spouse was cheating with someone he met from a secret dating app on the phone(Seeking Arrangements) I'm working on filing for a divorce right now so sad about but it’s only right. You can contact him if you have similar problems

Jack Davis dijo...

Quickbooks is the accounting software to handle business data. If you found any common error in this tool, you can download Quickbooks Tool Hub which is the combination of all tools in one application to save your time.
Quickbooks tool hub download

TERRY GREGOR dijo...

I have been sticking around the blogs and guestbook but I have never met an hacker and private investigator as fast and discreet as the WHITEHATS ORGANIZATION. These set of superior Cyber terrorist Known as "Whitehatspytech@cyberservices.com" have been in service for more than a decade and they have set an extremely impressive records that no Hacker can break,  they have the best hacking tools any one can ever imagine, that is why their job is performed with ease, contact them now for any of your hacking services like phone hacks, phone spy, social platform hack, bank loading, credits card loads and many more…You will be sure tothank me later

Dr, Benjamin Owen dijo...

AFFORDABLE/COMFORTABLE LOAN OFFER WITHOUT COST/STRESS CONTACT US VIA Call/Text +1(646)820-1981 WhatsApp: +19292227023 Email drbenjaminfinance@gmail.com
Loan Company Website: https://capitalmanage-inc.com/

Do you find yourself in a bit of trouble with unpaid bills and don’t know which way to go or where to turn? What about finding a reputable Debt Consolidation firm that can assist you in reducing monthly installment so that you will have affordable repayment options as well as room to breathe when it comes to the end of the month and bills need to get paid? ®Capital Managements Inc™ is the solution to your financial misfortune. We offer all types of loan ranging from $5,000.00 to $533,000,000.00USD with a low interest rate of 2% and loan duration of 1 to 35 years to pay back the loan secure and unsecured. Our loans are well insured for maximum security is our priority, Our leading goal is to help you get the services you deserve, Our program is the quickest way to get what you need in a snap. Kindly reduce your payments to ease the strain on your monthly expenses by Contacting us on the following WhatsApp:+19292227023 Email drbenjaminfinance@gmail.com

Loan Offer Alert For Everyone! Are you financially down and you need an urgent credit/financial assistance? Or are you in need of a loan to start-up/increase your business or buy your dream house. GET YOUR INSTANT LOAN APPROVAL 100% GUARANTEED TODAY NO MATTER YOUR CREDIT SCORE. drbenjaminfinance@gmail.com
Capital Managements Inc Website: https://capitalmanage-inc.com/