Mule is an open-source ESB, although there is a clear push to use their "Enterprise" commercial version instead. The documentation is ok, and there's a pretty good book
"Mule in Action".
Basic configuration of Mule is simple enough, and configuring a system to pull a text message from a JMS queue, perform some logic against the text, and push the message out to an SMTP server, isn't difficult, but the problem I wanted to solve was
HOW DO I TEST IT?
I don't want to have to fire up a full-blown JMS service and an SMTP server just to confirm that nobody has broken the Mule configuration prior to deployment. I also need to be sure that the correct email was sent, and I don't want to point my web-browser at GMail in order to check!
The "mule" way to do this testing is to replace these services with lightweight "in-memory" versions, run tests, and then examine behaviour as recorded by the lightweight services.
Instead of Weblogic for JMS, use an in-memory version of
Apache-MQ. Instead of a real SMTP server, use
"GreenMail"
Split the configuration into pieces.
- A core file that contains the services
- A file that contains Mule XML configuration appropriate for unit tests
- A file that contains Mule XML configuration appropriate for production
- A file that contains configuration properties appropriate for unit tests
- A file that contains configuration properties appropriate for production
eg:
A core file that contains the services
A file that contains Mule XML configuration appropriate for unit tests
Note that the jms definition fires up Apache Active-MQ inside a virtual machine (vm://localhost), but that the smtp definition is very generic.
A file that contains Mule XML configuration appropriate for production
Note that we are using a Weblogic JMS queue definition here, rather than the Apache MQ definition used in our test file.
A file that contains configuration properties appropriate for unit tests
A file that contains configuration properties appropriate for production
The unit test
Now we take a quick look at the unit test, which just fires up an in-memory SMTP server running on port 3025, pushes a message into the in-memory JMS queue, and checks that the expected email is sent using the "Greenmail" apis:
I actually attempted to write generic stub "connector" classes that my tests could interrogate, but Mule's endpoint classes make assumptions about the connector classes they are connected to (ie: they cast the "connector" that Mule passes them into, for example, org.mule.api.transport.JmsConnector), which meant they blew up when passed my stub connector.
No comments:
Post a Comment