簡體   English   中英

Spring Integration單元測試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"/>

具體來說,我們想要..

  1. 聲明要發送的對象的序列化; message-converters是否正確處理來自request-channel消息?
  2. 驗證來自第三方服務的響應處理; 給定各種響應(預期和意外)和錯誤(內部和外部)時的行為是什么?

我們有許多單元測試可以模擬出端點,並斷言我們的集成工作流的步驟可以按預期運行。 類似於以下內容:

@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 ,因此我們沒有對其進行測試。
  • 我們不想部署真正的HTTP服務來與之交互,這更是一項集成測試。
  • 第三方服務僅由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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM