Enterprise Integration Zone is brought to you in partnership with:

Pascal is a senior JEE Developer and Architect at 4Synergy in The Netherlands. Pascal has been designing and building J2EE applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Specialties: JEE XML Web Services Mule ESB Maven Cloud Technology Pascal is a DZone MVB and is not an employee of DZone and has posted 56 posts at DZone. You can read more from them at their website. View Full User Profile

Unit Test Mule 2 with ActiveMQ Defined as the Datasource

01.24.2013
| 2314 views |
  • submit to reddit
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!
Published at DZone with permission of Pascal Alma, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)