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

  • JMS UnitTest

    Hi
    The following applicationContext-jms.xml inside our application:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           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.0.xsd">
    
    
        <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
            <property name="environment">
                <props>
                    <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>
                </props>
            </property>
        </bean>
    
        <!--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"/>
        </bean>
         
        <!--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"/>        
        </bean>
    
        <!--destination for sending emails-->
        <bean id="sendEmailDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiTemplate" ref="jndiTemplate"/>
            <property name="jndiName" value="queue/SendMailQueue"/>
        </bean>
    
        <!--MDP listener bean, consume and send emails-->
        <bean id="emailMessageListenerMDP" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
            <constructor-arg>
                <bean class="com.supportspace.stargate.jms.email.MailMDP">
                    <property name="mailEngine" ref="mailEngine"/>
                    <property name="mailManager" ref="mailManager"/>
                </bean>
            </constructor-arg>
        </bean>
    
        <!--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"/>
        </bean>
    
    </beans>
    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 com.supportspace.stargate.jms.email.MailMDP 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.

    Comment

    Working...
    X