简体   繁体   English

模拟JmsTemplate进行集成测试

[英]Mock JmsTemplate for integration testing

Need to mock JmsTemplate for integration testing in my application. 需要在我的应用程序中模拟JmsTemplate以进行集成测试。

In my appcontext.xml 在我的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. 需要在testcontext.xml中模拟jmstemplete。 Thanks in advance. 提前致谢。

Or in Spring 4 flavour ;) 或者在春季4味;)

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

Exactly as @Stephane said, but without xml. 正如@Stephane所说,但没有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. 您可能需要在测试中注入模板并配置模拟( given(...).willReturn )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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