试图弄清楚如何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"/>
具体来说,我们想要..
- 断言正在发送的对象的序列化;是否
message-converters
正确处理来自request-channel
? - 验证来自 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 请求?