Over a million developers have joined DZone.
Platinum Partner

Unit Test Mule 2 with ActiveMQ Defined as the Datasource

· Integration Zone

The Integration Zone is brought to you in partnership with 3scale. Discover why Rails + jspm >= ECMAScript 6 awesomeness.

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!

The Integration Zone is brought to you in partnership with 3scale. Learn how to deploy an NGINX API gateway on Heroku.


Published at DZone with permission of Pascal Alma , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}