【问题标题】:Spring Integration: JUnit test RouterSpring 集成:JUnit 测试路由器
【发布时间】:2014-04-04 19:23:54
【问题描述】:

我想知道是否有一种方法可以让 Junit 测试 spring 集成路由器,而不必在 spring 集成示例项目中所示的两个不同的上下文文件中定义输入和映射通道?

我的项目中定义了多个路由器。我无法创建这么多上下文文件。

    <int:gateway id="accountBuilder"
    service-interface="some.package.AccountGateway" default-request-channel="accountRequest"  default-reply-channel="allAccounts"/>

<int:channel id="accountRequest"/>
<int:channel id="allAccounts"/>
<int:splitter input-channel="accountRequest" output-channel="accountRequests" ref="accountSplitter" method="split"/>

<int:channel id="accountRequests">
    <int:dispatcher task-executor="accountServiceTaskExecutor"/>
</int:channel>

<int:router input-channel="accountRequests" ref="accountRouter" method="routeAccountRequests">
    <int:mapping channel="accountType1HeaderEnricher"/>
    <int:mapping channel="accountType2HeaderEnricher"/>
    <int:mapping channel="accountType3HeaderEnricher"/>
    <int:mapping channel="accountType4HeaderEnricher"/>
</int:router>

<bean id="accountMessageStore" class="org.springframework.integration.store.SimpleMessageStore" />

<bean id="searchResultMessageStoreReaper" class="org.springframework.integration.store.MessageGroupStoreReaper">
    <property name="messageGroupStore" ref="accountMessageStore" />
    <property name="timeout" value="2000" />
</bean>


<int:channel id="accountType1HeaderEnricher"/>
<int:header-enricher input-channel="aAccountType1HeaderEnricher" output-channel="retailRequest">
    <int:header name="accountType1CorrelationId" expression="headers.correlationId" />
    <int:header name="accountType1SequenceSize" expression="headers.sequenceSize"/>
    <int:header name="accountType1SequenceNumber" expression="headers.sequenceNumber"/>
</int:header-enricher>

<int:channel id="account1Request"/>

<int:splitter input-channel="account1Request" output-channel="account1Requests" ref="account1Splitter" method="split"/>

<int:channel id="account1Requests">
    <int:dispatcher task-executor="accountServiceTaskExecutor"/>
</int:channel>

<int:router input-channel="account1Requests" ref="account1Router" method="routeAccount1Requests" default-output-channel="aggregatedAccount1HeaderEnricher">
    <int:mapping channel="account1Request1"/>
    <int:mapping channel="account1Request2"/>
</int:router>

【问题讨论】:

  • 其实这取决于你要测试什么。也许实例化MethodInvokingRouter 并调用它的handleMessage 就足够了。请展示你的情况
  • 您好,Artem,感谢您的回复。我已经用代码 sn-p 更新了这个问题。基本上,我有两级路由。第一个路由到不同的帐户类型,对于每个帐户类型,第二级路由器路由到不同的请求。所有都在同一个文件中定义。我需要单独对这些路由器进行单元测试,而不需要将其他路由器投入使用。

标签: spring-integration


【解决方案1】:

好的。只要您的所有频道都是直接的,或者换句话说 SubscribableChannel 并且您订阅了具体的端点,您可以简单地用 auto-startup="false" 标记它们,并将 JUnit 测试代码中的存根处理程序提供给这些频道,这适用于每个测试用例。或者使用 id 在所需端点上调用 start()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多