spring - 如何测试 Spring 集成

标签 spring spring-integration

我是 Spring Integration 的新手。我有 ActiveMQ,有一个“responseQ”。因此,当消息到达“responseQ”时 -> painResponseChannel -> Transformer -> processResponseChannel -> beanProcessing。我有以下设置:

    <jms:message-driven-channel-adapter  extract-payload="true"
                                     channel="painResponseChannel"
                                     connection-factory="connectionFactory"
                                     destination-name="responseQ"/>

    <integration:channel id="painResponseChannel" />

    <integration-xml:unmarshalling-transformer
        id="defaultUnmarshaller"
        input-channel="painResponseChannel"
        output-channel="processResponseChannel"
        unmarshaller="marshaller"/>

    <integration:channel id="processResponseChannel" />

    <integration:service-activator
        input-channel="processResponseChannel"
        ref="processResponseActivator"/>

    <bean id="processResponseActivator" class="com.messaging.processor.PainResponseProcessor"/>


    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
      <property name="classesToBeBound">
        <list>
            <value>com.domain.pain.Document</value>
        </list>
      </property>
    </bean>

所以我的问题是如何端到端地测试这个?如何断言变压器的输出或断言 channel 上的内容?我尝试过但失败了...希望有人能提供帮助。

提前致谢。 总经理

我是这样测试的:在我的测试上下文中创建了一个出站 channel 适配器,它启动使用 testJmsQueue channel 在 activeMQ 上放置消息。并且还为 processResponseChannel -> testChannel 创建了一个 BRIDGE。我期待 receive() 方法能给我一些返回。但我认为问题在于它太快了,当它到达 receive() 方法时,管道已经结束。

测试上下文如下所示:

<integration:bridge input-channel="processResponseChannel" output-channel="testChannel"/>

<jms:outbound-channel-adapter id="jmsOut" destination-name="responseQ" channel="testJmsQueue"/>

<integration:channel id="testJmsQueue"/>

<integration:channel id="testChannel">
    <integration:queue/>
</integration:channel>

然后在单元测试中我有这个:

@ContextConfiguration(locations = "classpath*:PainResponseTest-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class PainResponseTest {

private String painResponseXML;

@Autowired
MessageChannel testJmsQueue;
@Autowired
QueueChannel testChannel;

@Before
public void setup() throws Exception {

    ClassPathResource cpr = new ClassPathResource("painResponse.xml");
    InputStream is = cpr.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    painResponseXML = writer.toString();
}

@Test
@SuppressWarnings("unchecked")
public void shouldDoSomething() throws InterruptedException {

    testJmsQueue.send(MessageBuilder.withPayload(painResponseXML).build());

    Message<String> reply = (Message<String>) testChannel.receive(0);
    Assert.assertNotNull("reply should not be null", reply);
    String out = reply.getPayload();
    System.out.println(out);
}
}

==================== TEST OUTPUT =====================
java.lang.AssertionError: reply should not be null

收到的回复为空。

最佳答案

对于端到端测试,您可以执行以下操作;

  • 在嵌入式配置中使用 activemq 发送 JMS 消息
  • 在 processResponseChannel 上注入(inject) channel 拦截器
  • 启用DEBUG级别 - Spring Integration提供了非常好的和有用的日志来跟踪 channel 和服务激活器进出的消息

关于spring - 如何测试 Spring 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16219589/

相关文章:

java - 将 @Gateway 与 Spring-Integration-Kafka 一起使用

java - Spring Integration Java Config/DSL 中没有方法的服务激活器

java - 如何在 Spring 集成中使用 Java DSL 创建 ws 入站网关?

java - 如何从 ServerResponse 获取字符串形式的正文进行测试?

java - JWT token 问题

java - MultipartResolver 和 AbstractAnnotationConfigDispatcherServletInitializer

spring - 基于 HTTPS/SSL 的 AWS ELB 背后的 Zuul

java - 如何在没有数据库连接的情况下部署Spring应用程序

error-handling - Spring 集成 : How to use an "error gateway" with Splitter Aggregator when Exception thrown

spring-integration - 查询 Spring 集成最近处理的消息