Mock JmsTemplate for integration testing

sp_user123 picture sp_user123 · Aug 7, 2014 · Viewed 9.2k times · Source

Need to mock JmsTemplate for integration testing in my application.

In my appcontext.xml

<bean id="core_connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate">
            <ref bean="core_jndiTemplate" />
        </property>
        <property name="jndiName">
            <value>ConnectionFactory</value>
        </property>
    </bean>

    <bean id="core_jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="core_connectionFactory" />
        <property name="defaultDestination" ref="core_destination" />
    </bean>


    <bean id="core_destination" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate">
            <ref bean="core_jndiTemplate" />
        </property>
        <property name="jndiName">
            <value>queue/CoreQueue</value>
        </property>
    </bean>

need to mock the jmstemplete in my testcontext.xml. Thanks in advance.

Answer

G. Demecki picture G. Demecki · Jan 20, 2015

Or in Spring 4 flavour ;)

@Bean
public JmsTemplate jmsTemplate() {
    return Mockito.mock(JmsTemplate.class);
}

Exactly as @Stephane said, but without xml.
But still I would recommend you use an embedded broker for your integration tests. As it would allow you to check what exactly is coming to the queue.