Spring Dependency Injection in Mule FunctionalTestCase

By | December 13, 2015

In this article I will show how to accomplish dependency injection of fields in tests that inherit from the Mule class FunctionalTestCase; both of dependencies to Spring beans and of values from property files.


On an earlier occasion, I have attempted to use the standard @RunWith(SpringJUnit4ClassRunner.class) annotation in order to be able to have Spring inject dependencies into my tests. This works very well as long as your test-class does not inherit from the Mule FunctionalTestCase class.
Instead I have had to rely on writing code to retrieve dependencies and configuration property values in such tests.
In Mule 3.7 the Mule registry has been unified  and now all Spring beans (i.e. objects, but I have never seen anyone use anything but Spring as the DI container in Mule) can be found in one and the same registry.

Attempt with Null

Let’s get down to business, that is the code. This is how I would like to be able to write a Mule test based on the class FunctionalTestCase:

Worth mentioning is that I have a properties file with the following contents:

However, if we run the above test, the following will be printed to the console:

A New Test Class

How does Spring accomplish dependency injection of fields annotated with, for instance, @Autowired?
Some research led me to the AutowiredAnnotationBeanPostProcessor class in Spring. In that class I even found a method named processInjection which sounded just right! Some research into Mule’s FunctionalTestCase and its parent classes led me to decide that the doSetUp method seems like a suitable place to perform dependency injection of the test instance. Armed with this knowledge, I wrote a new parent-class for my Mule tests:

I added some checks and warning logs, in case the necessary prerequisites are not met or a plain error occurs.

Attempt with the New Test Class

If I now change the parent class of my test class shown earlier from FunctionalTestCase to SpringDIFunctionalTestCase and run it, the following is printed on the console:


The complete example project is available on GitHub.
Happy coding!





Leave a Reply

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