试图找出如何在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的消息?我们已经有了许多单元测试,这些测试模拟出了端点,并断言了集成工作流的步骤与预期的行为。如下所示:
@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,那么我们就不能测试它。url-expression解析,因此没有一个Spring可以模拟。也许我们可以拦截HTTP请求Spring尝试发送的?
发布于 2015-01-15 13:37:55
在框架测试中,我们使用DirectFieldAccessor将端点的RestTemplate替换为模拟(实际上是存根)。然而,这并不能测试转换器。
您可以变得更加复杂,在那里可以测试真正的RestTemplate;只需获得对它的引用(使用SI TestUtils.getPropertyValue()或DirectFieldAccessor),并按照Spring框架文档中讨论的那样配置它。
您可以使用bean名称endpointId.handler获得对处理程序的引用。
https://stackoverflow.com/questions/27961588
复制相似问题