![](/img/trans.png)
[英]Spring Integration - url-variable not working in http:outbound-gateway
[英]Spring Integration unit test http:outbound-gateway
試圖弄清楚如何在Spring Integration工作流中最好地對http:outbound-gateway
進行單元測試。
這是我們的網關的外觀:
<int-http:outbound-gateway id="gateway"
request-channel="registrationQueue"
message-converters="jsonMessageConverter"
url-expression="@urlGenerator.resolve()"
http-method="POST"
expected-response-type="javax.ws.rs.core.Response"
reply-channel="nullChannel"
error-handler="httpResponseErrorHandler"/>
具體來說,我們想要..
message-converters
是否正確處理來自request-channel
消息? 我們有許多單元測試可以模擬出端點,並斷言我們的集成工作流的步驟可以按預期運行。 類似於以下內容:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-config.xml"})
public class FileRegistrationWorkflowTest {
...
@Autowired
private MessageChannel fileFoundChannel;
@Autowired
private QueueChannel testRegistrationQueue;
...
@Test
public void shouldQueueRegistrationForFileWithEntityId() {
// Given
mockFileLookupService(FILE_ID, FILENAME_WITH_ENTITY_ID);
// When
fileFoundChannel.send(MessageBuilder.withPayload(FILE_ID).build());
// Then
Message<?> message = testRegistrationQueue.receive();
assertThat(message, hasPayload(expected));
}
}
這種測試方法非常適合工作流中的各個步驟。 我們的麻煩是測試端點網關。
http:outbound-gateway
,因此我們沒有對其進行測試。 url-expression
解析,因此沒有Spring Bean可模擬。 也許我們可以攔截 Spring嘗試發送的HTTP請求?
在框架測試中,我們使用DirectFieldAccessor
用模擬(實際上是存根)替換端點的RestTemplate
。 但是,這不會測試轉換器。
您可以變得更加復雜,可以在其中測試真正的RestTemplate
。 只需獲取它的引用(使用SI TestUtils.getPropertyValue()
或DirectFieldAccessor
)並按照Spring Framework文檔中的討論進行配置即可。
您可以使用bean名稱endpointId.handler
獲得對處理程序的引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.