Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Unit Test Mule 2 with ActiveMQ Defined as the Datasource

DZone's Guide to

Unit Test Mule 2 with ActiveMQ Defined as the Datasource

· Integration Zone
Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements. What's below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

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"
           xmlns:amq="http://activemq.apache.org/schema/core"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation=
                   "http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    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"/>
    </beans>

    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:

<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-core</artifactId>
  <version>5.2.0</version>
   <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.apache.xbean</groupId>
  <artifactId>xbean-spring</artifactId>
  <version>2.8</version>
  <scope>test</scope>
</dependency>
That’s it. Now you can test your code using an embedded version of ActiveMQ. Happy testing!

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:

Published at DZone with permission of Pascal Alma. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}