【发布时间】: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"/>
具体来说,我们想要..
- 断言正在发送的对象的序列化;
message-converters是否正确处理来自request-channel的消息? - 验证来自第 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