[英]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.. 具体来说,我们想要..
message-converters
correctly process messages coming from the request-channel
? message-converters
是否正确处理来自request-channel
消息? 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..
我们的麻烦是测试端点网关。
http:outbound-gateway
, then we aren't testing it. http:outbound-gateway
,因此我们没有对其进行测试。 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.