Mock JmsTemplate for Integration Testing

Need to mock JmsTemplate for testing integration 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 make fun of jmstemplete in my testcontext.xml. Thanks in advance.

+3


source to share


2 answers


Or in Spring 4 flavors;)

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

      



Exactly as @Stephane said, but without the xml.
But still I would recommend using the built-in broker for your integration tests. As this will allow you to check what exactly comes up in the queue.

+5


source


How about the next one?

<bean id="core_jmsTemplate" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="org.springframework.jms.core.JmsTemplate"/>
</bean>

      



You will probably need to inject a template and adjust the layout ( given(...).willReturn

) in your test.

+2


source







All Articles