[英]Problems with unit testing a spring integration program
我正在嘗試對xpath路由器進行單元測試,但是有問題。 這是我的上下文文件:
<int:channel id="toTransactionTypeRouterChannel" />
<int-xml:xpath-router id="transactionsTypeRouter"
input-channel="toTransactionTypeRouterChannel" resolution-required="false"
evaluate-as-string="true" default-output-channel="errorChannel">
<!-- Select node name of the first child -->
<int-xml:xpath-expression
expression="name(/soapNs:Envelope/soapNs:Body/schNs:processArchiveRequest/schNs:fulfillmentRequest/schNs:requestDetail/*[1])"
namespace-map="archiveNamespaceMap" />
<int-xml:mapping value="sch:bulkRequestDetail"
channel="bulkChannel" />
<int-xml:mapping value="sch:transactionalRequestDetail"
channel="transactionChannel" />
</int-xml:xpath-router>
<int:channel id="bulkChannel" />
<int:channel id="transactionChannel" />
<int:transformer input-channel="bulkChannel"
output-channel="consoleOut" expression="'Bulk channel has received the payload' " />
<int:transformer input-channel="transactionChannel"
output-channel="consoleOut" expression="'Transaction channel has received payload' " />
<int:transformer input-channel="errorChannel"
output-channel="consoleOut" expression="'Error channel has received payload' " />
如您所見,這里有2條不同的路由(批量,傳輸)+錯誤通道。這是我的跨通道路由的單元測試用例:
@Test
public void testTransactionFlow() throws Exception {
try {
Resource bulkRequest = new FileSystemResource("src/main/resources/mock-message-examples/SampleProcessArchiveTransRequest.xml");
String transRequestStr= extractResouceAsString(bulkRequest);
toTransactionTypeRouterChannel.send(MessageBuilder.withPayload(transRequestStr).build());
Message<?> outMessage = testChannel.receive(0);
assertNotNull(outMessage);
junit的上下文文件
<int:bridge input-channel="transactionChannel"
output-channel="testChannel"/>
<int:channel id="testChannel">
<int:queue/>
</int:channel>
如您所見,在junit上下文文件中,我將事務通道連接到測試通道。在junit測試用例中,我正在通過junit方法向路由器發送有效負載,並嘗試從輸入通道和用它來斷言。 但是斷言失敗,因為來自事務通道的消息在轉到junit coontext文件中給定的路由到inputChannel之前直接進入consoleOut。 在進入consoleOut之前如何截獲消息? 我還嘗試添加wireTap攔截器,但它們沒有起作用:
WireTap wireTap = new WireTap(someChannel);
boolean w = wireTap.isRunning();
transactionChannel.addInterceptor(wireTap);
基本上,我需要一個單獨的流程來進行單元測試。
使用該配置,您只需向transactionChannel
添加第二個使用者-消息將循環分發到轉換器和網橋。
您可以通過以id
作為EventDrivenConsumer
自動裝配它並在發送消息之前對其進行stop()
來取消訂閱該測試用例的轉換器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.