简体   繁体   English

Spring Integration单元测试http:outbound-gateway

[英]Spring Integration unit test http:outbound-gateway

Trying to figure out how to best unit test an http:outbound-gateway in a Spring Integration workflow. 试图弄清楚如何在Spring Integration工作流中最好地对http:outbound-gateway进行单元测试。

Here's what our gateway looks like: 这是我们的网关的外观:

<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"/>

Specifically, we want to.. 具体来说,我们想要..

  1. Assert serialization of the objects being sent; 声明要发送的对象的序列化; do the message-converters correctly process messages coming from the request-channel ? message-converters是否正确处理来自request-channel消息?
  2. Verify response handling from the 3rd party service; 验证来自第三方服务的响应处理; what is the behavior given various responses (expected & unexpected) and errors (internal & external)? 给定各种响应(预期和意外)和错误(内部和外部)时的行为是什么?

We've got a number of unit tests that mock out the end points and assert the steps of our integration workflow behave as expected. 我们有许多单元测试可以模拟出端点,并断言我们的集成工作流的步骤可以按预期运行。 Something like the following: 类似于以下内容:

@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));
    }

}

This method of testing works great for the steps along the workflow. 这种测试方法非常适合工作流中的各个步骤。 Our trouble is testing the the end point gateways.. 我们的麻烦是测试端点网关。

  • We can't mock the http:outbound-gateway , then we aren't testing it. 我们无法模拟http:outbound-gateway ,因此我们没有对其进行测试。
  • We don't want to deploy a real HTTP service to interact with, that's more an integration test. 我们不想部署真正的HTTP服务来与之交互,这更是一项集成测试。
  • The 3rd party service is only resolved by the url-expression , so there isn't a Spring bean to mock out. 第三方服务仅由url-expression解析,因此没有Spring Bean可模拟。

Perhaps we can intercept the HTTP request Spring tries to send? 也许我们可以拦截 Spring尝试发送的HTTP请求?

In the framework tests we use a DirectFieldAccessor to replace the endpoint's RestTemplate with a mock (actually a stub). 框架测试中,我们使用DirectFieldAccessor用模拟(实际上是存根)替换端点的RestTemplate However, this doesn't test the converters. 但是,这不会测试转换器。

You can get even more sophisticated, where the real RestTemplate can be tested; 您可以变得更加复杂,可以在其中测试真正的RestTemplate just get a reference to it (with the SI TestUtils.getPropertyValue() or a DirectFieldAccessor ) and configure it as discussed in the Spring Framework documentation . 只需获取它的引用(使用SI TestUtils.getPropertyValue()DirectFieldAccessor )并按照Spring Framework文档中的讨论进行配置即可。

You can get a reference to the handler with bean name endpointId.handler . 您可以使用bean名称endpointId.handler获得对处理程序的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Integration-网址变量在http:outbound-gateway中不起作用 - Spring Integration - url-variable not working in http:outbound-gateway Spring Integration http出站网关和UTF-8 - Spring Integration http outbound-gateway and UTF-8 Spring Integration http:outbound-gateway“没有合适的HttpMessageConverter” - Spring Integration http:outbound-gateway “no suitable HttpMessageConverter” Spring Integration-占位符未注入ws:outbound-gateway uri(仅用于单元测试) - Spring Integration - placeholders not injected into ws:outbound-gateway uri (only for unit tests) Spring Integration Java DSL - 如何调用int-http:outbound-gateway? - Spring Integration Java DSL - How to invoke int-http:outbound-gateway? 在int-ws:outbound-gateway上通过Spring集成处理Http错误 - Handle Http errors with spring integration on int-ws:outbound-gateway Spring Integration:如何使用http:outbound-gateway将字节数组作为POST参数发送? - Spring Integration: how to send a Byte Array as POST parameter with an http:outbound-gateway? 如何在Spring集成中使用JAVA配置创建http outbound-gateway? - How can I create http outbound-gateway with JAVA config in Spring integration? Spring Integration通过出站网关递归获取FTP文件 - Spring Integration get FTP files recursively with outbound-gateway Spring集成出站网关想像动态一样使用URL - Spring integration outbound-gateway want to use URL as dynamic like
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM