简体   繁体   中英

Mock JmsTemplate for integration testing

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.

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.

How about the following?

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

You probably need to inject the template and configure the mock ( given(...).willReturn ) in your test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM