2

试图弄清楚如何http:outbound-gateway在 Spring Integration 工作流中最好地进行单元测试。

这是我们的网关的样子:

<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. 验证来自 3rd 方服务的响应处理;给出各种响应(预期和意外)和错误(内部和外部)的行为是什么?

我们有许多单元测试来模拟端点并断言我们的集成工作流的步骤按预期运行。类似于以下内容:

@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 服务来与之交互,这更像是一个集成测试。
  • 3rd 方服务仅由 解析url-expression,因此没有可模拟的 Spring bean。

也许我们可以拦截Spring 尝试发送的 HTTP 请求?

4

1 回答 1

3

框架测试中,我们使用 aDirectFieldAccessor将端点替换RestTemplate为模拟(实际上是存根)。但是,这不会测试转换器。

您可以变得更复杂,RestTemplate可以测试真实的地方;只需获取对它的引用(使用 SITestUtils.getPropertyValue()或 a )并按照Spring Framework 文档DirectFieldAccessor中的讨论对其进行配置。

您可以使用 bean name 获取对处理程序的引用endpointId.handler

于 2015-01-15T13:37:55.530 回答