I checked in my changes, time to wait for the build to be completed in jenkins. It was time to pick up coffee and play the waiting game, as it would take another hour before I can proceed with deployment of the code. We obviously had to do something !!
Mapping the wastage
This micro service was responsible for sending/receiving mails from different configured mail servers. We were doing unit and integration level testing, where in integration testing we were connecting to a local exchange server with pre-configured test users for sending/receiving mail.
The best way to start optimization - find gaps & wastage. We broke down the total build execution into build, deployment on staging environment & acceptance.
We noticed the build & deployment time was fair, however the acceptance tests were taking around 40-45 min to complete. Now that we have narrowed our focus. It was time to do a breakdown analysis of test.
We often have been observing slowness of builds, sometimes intermittent failures as well. The reason were obvious, tests were using same set of users, so during parallel execution data gets corrupted. We needed a clean way test our api’s. Also after regular intervals, we had to clean the mail accounts.
To put simply we needed
- Unique set of user for each test.
- Local server to reduce network delays.
- Ability to automatically flush the existing data.
- Behave like a real world mail server
- Supports the standards protocols - SMTP, IMAP & POP3
GreenMail to rescue
Time for us to switch to mock email server, there are good number of servers available. Some are free to use and some are paid.
GreenMail is open source while other solutions are paid. As stated on the main page, the benefits include:
- Supports SMTP, POP3 and IMAP including SSL
- Prevents accidental email leaking to real mail servers
- Provides different deployment models, such as a simple standalone JVM process, as a WAR module or as a docker image
- Easily embeddable in JUnit tests for integration testing
This library supports both receiving and retrieving mails from java.
Java Mail Service
We will create a simple method to mimic a real world functionality - to send a mail.
So now we need to test this functionality, as its clear we will need a email server to validate if our code is working fine.
Set up GreenMail
We will embed a mock server in our test, first we will add a test dependency for greenmail
Next, we are going to write a simple test to send message and validate. In this example, we will start our mock server at 9000 port and connect to it via our api, and send the message.
With every test I can create unique set of users, and run my test against it and finally purge the server.
GreenMail also gives utility to access the received messages. You can also use the standard way by connecting via IMAP/POP3 implementations.
Its a great way to avoid message spill over, cleanup managed exchange server and improves reliability.