【问题标题】:Spring Integration unit test http:outbound-gatewaySpring 集成单元测试 http:outbound-gateway
【发布时间】:2015-01-15 10:41:18
【问题描述】:

试图弄清楚如何在 Spring 集成工作流中对 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. 验证来自第 3 方服务的响应处理;给定各种响应(预期和意外)和错误(内部和外部),会有什么行为?

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

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

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

【问题讨论】:

    标签: java spring unit-testing spring-integration outbound


    【解决方案1】:

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

    您可以变得更加复杂,可以测试真正的RestTemplate;只需获取对它的引用(使用 SI TestUtils.getPropertyValue()DirectFieldAccessor)并按照 Spring Framework documentation 中的讨论进行配置。

    您可以获得对带有 bean 名称 endpointId.handler 的处理程序的引用。

    【讨论】:

    • 太棒了,听起来像我要找的东西,谢谢 Gary。
    猜你喜欢
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多