【发布时间】:2016-11-22 16:04:39
【问题描述】:
假设我有这样一条 apache 骆驼路线:
from("direct:start")
.routeId("aRouteId")
.bean(someBusinnessTransformationBean).id("transformationBean")
.bean(aPersistenceBean).id("persistenceBean")
.to("direct:target");
然后,在我的单元测试中,我正在做类似的事情:
public class RouteTest extends CamelTestSupport {
@Override
public boolean isDumpRouteCoverage() { return true }
@Override
public boolean isUsedAdviceWith() { return true }
@EndpointInject(uri = "mock:mockTransformationBean")
protected MockEndpoint mockTransformationBean;
@EndpointInject(uri = "mock:mockPersistenceBean")
protected MockEndpoint mockPersistenceBean;
@Test
public void testRoute() throws Exception {
context.getRouteDefinition("aRouteId").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("transformationBean")
.replace()
.multicast()
.to(mockTransformationBean);
weaveById("persistenceBean")
.replace()
.multicast()
.to(mockPersistenceBean);
}
});
context.start();
// Asserts the expectedMessageCount
// Send a message with template.sendBody...
assertMockEndpointSatisfied();
}
}
问题:
我怎样才能正确地对该路由进行单元测试,而不仅仅是确保 messageCount 和接收到的正文(在第一个 bean 上)。
例如,我如何像使用 Mockito 进行单元测试一样模拟 MockEndpoint 的响应:
when(mockTransformationBean.someHandler(...)).thenReturn(anExpectedObjectForMyUnitTestPropose);
干杯,尼古拉斯
【问题讨论】:
-
为什么不直接运行单元测试,然后将交换主体与一些预期主体进行比较?
-
因为我找不到用apache camel MockEndpoint拦截bean的交换体的方法。我能做的是确保接收到的主体到模拟,但是,我想确保第一个 bean 转换主体并将不同的主体发送到第二个 bean。
-
你可以在 bean 拦截后做一个 adviceWith 和发送到一个 mockendpoint。然后断言消息应该是什么。
标签: java unit-testing mocking apache-camel