如何在Spring集成中模拟连接工厂

时间:2018-05-22 15:46:44

标签: mocking jms spring-integration spring-jms

我想模拟JMS连接工厂进行集成测试。

<jee:jndi-lookup id="narconFactory" jndi-name="LBARQCF" resource-ref="false"/>

何时(?)

我们使用websphere服务器,而对于intyegration测试则需要使用tomcat。 Spring引导版本是1.5.3

修改: 根据评论,我创建了一个类

@RunWith(SpringRunner.class)
@SpringBootTest
public class MQControllerTest {

//step 2
@Autowired
private MockMvc mvc;

@MockBean
private MQController mqController;

protected static final ConnectionFactory amqFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

public static final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(amqFactory);

@BeforeClass
public static void startUp() throws Exception {
    connectionFactory.setCacheConsumers(false);
    connectionFactory.createConnection().close();
}

@AfterClass
public static void shutDown() {
    connectionFactory.resetConnection();
}

@Autowired
private JmsTemplate mockJmsTemplate;

@Autowired
private SourcePollingChannelAdapter startJms;


@Before
public void setup() throws JMSException{
    Mockito.reset(this.mockJmsTemplate);
}   

@Test
public void test_tbm_vrijeDagenCorrectie() throws Exception {

    String expectedResponse ="";
    //using bddmockito
    given(mqController.tmVDCorrect(any(String.class))).willReturn(expectedResponse);

    this.startJms.start();

    mvc.perform(get("/tm/vdc/{dgn}","2016-01-16"))
               .andExpect(status().isOk());

}

}

在我的资源中 - 配置文件**编辑***使用完整的包名称并将connectionFactory设为公共

    <util:constant id="narconFactory" static-field="n.d.poc.MQControllerTest.connectionFactory"/>
     <util:constant id="vanconFactory" static-field="n.d.poc.MQControllerTest.connectionFactory"/> 

<击> 错误:

<击>
  

没有'org.springframework.test.web.servlet.MockMvc'类型的限定bean可用:预计至少有1个bean可以作为autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}       在org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474)〜[spring-beans-4.3.5.RELEASE.jar:4.3.5.RELEASE]

不确定我的错误 EDIT2:

  

引起:org.springframework.beans.factory.BeanCreationException:   创建名为'narconFactory'的bean时出错:调用   init方法失败;嵌套异常是   javax.naming.NoInitialContextException:需要在中指定类名   环境或系统属性,或作为applet参数,或在   应用程序资源文件:java.naming.factory.initial at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)   〜[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)   〜[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)   〜[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)   〜[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)   〜[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)   〜[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)   〜[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742)   〜[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)   〜[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)   〜[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)   〜[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] at   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)   〜[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] at   org.springframework.boot.SpringApplication.run(SpringApplication.java:314)   〜[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] at   org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)   〜[spring-boot-test-1.5.3.RELEASE.jar:1.5.3.RELEASE] at   org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)   〜[spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE] at   org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)   〜[spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE] ... 27个常见帧   省略

为什么单元测试正在寻找java.naming.factory.initial?我可以提供应用程序属性和具有什么价值?

EDIT3:

  

github中的示例代码   https://github.com/kswat/LRIntegrationUnit

1 个答案:

答案 0 :(得分:1)

我建议考虑使用ActiveMQ的嵌入模式而不是模拟。通过这种方式,您将获得完全成熟的JMS,并且不会处理低级资源以进行模拟。

在Spring Integration中,我们将其用于测试目的:

/**
 * Keeps an ActiveMQ {@link VMTransport} open for the duration of
 * all tests (avoids cycling the transport each time the last
 * connection is closed).
 * @author Gary Russell
 * @since 3.0
 *
 */
public abstract class ActiveMQMultiContextTests {

    protected static final ConnectionFactory amqFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    protected static final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
            amqFactory);

    @BeforeClass
    public static void startUp() throws Exception {
        connectionFactory.setCacheConsumers(false);
        connectionFactory.createConnection().close();
    }

    @AfterClass
    public static void shutDown() {
        connectionFactory.resetConnection();
    }

}

connectionFactory静态属性确实可以用作bean:

<util:constant id="narconFactory"
               static-field="ActiveMQMultiContextTests.connectionFactory"/>