Unit Testing with the Validations Module in Mule ESB

By | July 1, 2016

In this post I will show a workaround for a bug that appears when testing Mule flows that contains one or more validators from the relatively new Validations Module. The bug has been verified to be present in both Mule ESB 3.7.0 and 3.8.0.
There are two issues in MuleSofts JIRA here and here, but the fix has only been applied to the DefaultMuleApplication class and thus won’t improve the situation when implementing tests using the class FunctionalTestCase as a parent class.

Create the Example Project

The example project, which is a regular Maven project, will first reproduce the bug and then propose a workaround.
Create an empty Maven project in your favourite IDE without using a Maven archetype. I have named my project “validator-unittesting”.

The pom.xml File

Replace the contents of the pom.xml file with the listing below. Note that this pom-file will not allow for more than running tests in the project.

Mule Configuration File

To make it easy for myself, I have placed the Mule configuration file used by the test in src/test/resoruces/mule-config.xml. It contains the following configuration:

Note that:

  • I have used a VM inbound endpoint in the main flow.
    This was since it is easier to send a request with a payload to a VM endpoint using the Mule client.
  • There is a <test:component> element.
    This will log all received request messages, including their metadata, to the console.
  • There is a <validation:is-not-null> element.
    This is a validation from the validations module that asserts that the expression in the value attribute does not evaluate to null.
    If the expression does evaluate to null, then a ValidationException will be thrown with the message from the message attribute of the <validation:is-not-null> element set as the exception message. In this case it is the payload of the received request that must not be null.

Log4J Configuration

A minimal Log4J2 configuration file was created in src/test/resources/log4j2-test.xml with the following contents just to get some logging output to the console while running the tests.

Unit-Test Class

The unit-test class was placed in src/test/java and is implemented as this:

There are two tests in this test-class; one that sends a request with a payload and another that sends a request with a null payload.

Reproducing the Bug

If we now run the test-class, both tests will fail with console output similar to this:

This looks very similar to the log output in the issue MULE-8487, which led me to believe that the extensions manager had also been omitted when starting an embedded instance of Mule in a test.

Workaround

I propose a workaround for the bug that consists of adding the following method to the test-class:

Note that the extensions manager builder is inserted first in the list of builders. This is because the extensions manager must be created before the Spring context is created, since the creation of the Spring context depends on an extension manager instance being available, since there is a validator in the Mule configuration file.

Successful Test

If the test-class is run again, both the tests should now succeed.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *