4

需要模拟 JmsTemplate 以在我的应用程序中进行集成测试。

在我的 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>

需要在我的 testcontext.xml 中模拟 jmstemplet。提前致谢。

4

2 回答 2

9

或春季4味;)

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

正如@Stephane 所说,但没有xml。
但我仍然建议您使用嵌入式代理进行集成测试。因为它可以让您检查队列中到底有什么。

于 2015-01-20T15:04:22.627 回答
2

下面的呢?

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

您可能需要在given(...).willReturn测试中注入模板并配置模拟 ( )。

于 2014-08-07T12:03:05.703 回答