Announcement Announcement Module
No announcement yet.
JMS UnitTest Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JMS UnitTest

    The following applicationContext-jms.xml inside our application:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
        <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
            <property name="environment">
                    <prop key="java.naming.provider.url">${java.naming.provider.url}</prop>
                    <prop key="java.naming.factory.url.pkgs">org.jnp.interfaces:org.jboss.naming</prop>
                    <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
        <!--reference to jboss jms connection factory , can use httpConnection factory-->
        <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiTemplate" ref="jndiTemplate"/>
            <property name="jndiName" value="ConnectionFactory"/>
        <!--JMSTemplate bean used to manage all connection and session, it's Spring way to overcome
        the JMS unnecessary repetitives code, set to non persistent mode-->
        <bean id="jmsGenaralTempalte" class="org.springframework.jms.core.JmsTemplate">
            <property name="connectionFactory" ref="connectionFactory"/>
            <property name="defaultDestination" ref="sendEmailDestination"/>
            <property name="deliveryPersistent" value="false"/>
            <property name="sessionTransacted" value="true"/>        
        <!--destination for sending emails-->
        <bean id="sendEmailDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiTemplate" ref="jndiTemplate"/>
            <property name="jndiName" value="queue/SendMailQueue"/>
        <!--MDP listener bean, consume and send emails-->
        <bean id="emailMessageListenerMDP" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
                <bean class="">
                    <property name="mailEngine" ref="mailEngine"/>
                    <property name="mailManager" ref="mailManager"/>
        <!--use DefaultMessageListenerContainer to support JTA transaction-->
        <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
            <property name="connectionFactory" ref="connectionFactory"/>
            <property name="destination" ref="sendEmailDestination"/>
            <property name="messageListener" ref="emailMessageListenerMDP"/>
            <property name="transactionManager" ref="transactionManager"/>
    When I tried to use this for running the unit tests all failed with initalization beans error,
    I need help with creating applicationTest.xml for that porpuse and I don't understand how to use rg.springframework.mock.jndi.ExpectedLookupTemplat e
    Please help

  • #2
    What you are describing is an integration test. ExpectedLookupTemplate is expected to be used in a unit test for an object that takes a JndiTemplate and needs to use it to look up particular objects.

    The way you have written your application, you have abstracted every bit of infrastructure away from your business logic, so unit testing your class shouldn't require a JndiTemplate or an XML file.

    If you wanted to create an integration test which wires it all together you'd need an implementation of JNDI and JMS. Its possible ActiveMQ can provide those in an embedded fashion. I haven't done it myself, however I have spoke to Rod Johnson about this topic at a conference a while ago and he suggested something like this.

    Usually for my application contexts I put my JNDI specific objects into another file. That way if I want to create an integration test, I can create a non-JNDI version of that file. I haven't done it with JMS, only JDBC. I use SingleConnectionDataSource in that other version of the file instead of using JNDI.