Unit Test Mule with ActiveMQ defined as datasource

In a previous post I described how you can configure an ActiveMQ connection-factory as datasource in Tomcat. Although this works great in a deployed Mule application you can’t use this technique in your unit tests! So in this post I describe how we set up our test environment.

  1. Load a different spring config file for your tests
  2. You have to make sure your unit test load the correct Spring configs. There are various ways to accomplish this. In our case we have created a base test class that loads the test-core config for the datasources.
    Here is the code of our base class:

    package net.pascalalma.mule.test;
    import org.apache.log4j.Logger;
    import org.mule.api.MuleException;
    import org.mule.module.client.MuleClient;
    import org.mule.tck.FunctionalTestCase;
    import org.mule.util.StringUtils;
    public abstract class BaseFunctionalTestCase extends FunctionalTestCase {
        protected static Logger logger = Logger.getLogger(BaseFunctionalTestCase.class);
         * Loading test-core-config.xml is default. Extra mule-config.xml can be loaded through getAdditionalConfigResources.
         * @return all mule configs to be loaded with this testcase
        protected final String getConfigResources() {
            return "config/test-core-config.xml " + StringUtils.defaultIfEmpty(getAdditionalConfigResources(), "");
         * Load additional mule-config.xml. Seperate multiple config files with a space.
         * @return
        protected abstract String getAdditionalConfigResources();
  3. Configure the ActiveMQ pool in your test Spring config file
  4. In the Spring config file that is loaded for unit testing put the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
                    http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
        <!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
        <amq:connectionFactory id="activeMqConnectionFactory" brokerURL="vm://localhost"/>

    You have to make sure the id of the defined connectionFactory matches the one defined in your Tomcat instance since that name will be used in your code.

  5. Add the necessary dependencies to your project
  6. For the unit test to work you need to add the following dependencies to your pom.xml:


    That’s it. Now you can test your code using an embedded version of ActiveMQ. Happy testing!

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Mule, Mule2 and tagged , , . Bookmark the permalink.