我是骆驼的新手,我需要了解如何对具有两个端点的路由进行单元测试。第一个端点获取用户ID,并将其用于第二个端点。
public RouteBuilder routeBuilder() {
return new RouteBuilder() {
@Override
public void configure() throws HttpOperationFailedException {
this.from(MyServiceConstant.ROUTE)
.setHeader(...)
.setHeader(...)
.to(MyConstants.THE_FIRST_ROUTE)
.setHeader(...)
.setHeader(...)
.process(...)
.setProperty(...)
.to(MyConstants.THE_SECOND_ROUTE)
}
};
}
因此,我必须在Test类中同时模拟MyConstants.THE_FIRST_ROUTE和MyConstants.THE_SECOND_ROUTE。我这样做了,但是不确定如何编写测试。我正在做的是到达第二个端点,但不知道如何触发第一个端点。
@Produce(uri = MyServiceConstant.ROUTE)
private MyService myService;
@EndpointInject(uri = "mock:" + MyConstants.THE_FIRST_ROUTE)
private MockEndpoint mockFirstService;
@EndpointInject(uri = ""mock:" + MyConstants.THE_SECOND_ROUTE)
private MockEndpoint mockSecondService;
@Test
@DirtiesContext
public void getDetails()throws Exception {
// **The missing part**: Is this the right way to call my first service?
this.mockFirstService.setUserId("123456");
// this returns a JSON that I'll compare the service response to
this.mockSecondService.returnReplyBody(...PATH to JSON file);
UserDetail userDetailsInfo = this.myService.getUserDetails(...args)
// all of my assertions
assertEquals("First name", userDetailsInfo.getFirstName());
MockEndpoint.assertIsSatisfied();
}
答案 0 :(得分:1)
这是Mock组件的单元测试用例的link。它显示了如何使用mock:
端点和CamelTestSupport
实施测试。 @ Roman Vottner在他的评论中完全正确。
This test case可能对您特别感兴趣,因为它显示了如何将smtp:
端点与mock:
端点交换。此外,here是有关如何模拟现有端点的官方文档(将它们用作测试探针)。
注意:请记住,在该区域中,Camel 3.0 API与Camel 2.x API完全不同。祝你好运!
答案 1 :(得分:1)
我今天有时间用Camel Spring启动原型快速破解一些演示代码。开始了。我的路线从timer
组件生成消息。不使用显式传递到端点。
//Route Definition - myBean::saySomething() always returns String "Hello World"
@Component
public class MySpringBootRouter extends RouteBuilder {
@Override
public void configure() {
from("timer:hello?period={{timer.period}}").routeId("hello_route")
.transform().method("myBean", "saySomething")
.to("log:foo")
.setHeader("test_header",constant("test"))
.to("log:bar");
}
}
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
public class MySpringBootRouterTest {
@Autowired
SpringCamelContext defaultContext;
@EndpointInject("mock:foo")
private MockEndpoint mockFoo;
@EndpointInject("mock:bar")
private MockEndpoint mockBar;
@Test
@DirtiesContext
public void getDetails() throws Exception {
assertNotNull(defaultContext);
mockBar.expectedHeaderReceived("test_header", "test");
mockBar.expectedMinimumMessageCount(5);
MockEndpoint.setAssertPeriod(defaultContext, 5_000L);
MockEndpoint.assertIsSatisfied(mockFoo, mockBar);
mockFoo.getExchanges().stream().forEach( exchange -> assertEquals(exchange.getIn().getBody(),"Hello World"));
//This works too
//mockBar.assertIsSatisfied();
//mockFoo.assertIsSatisfied();
}
@Before
public void attachTestProbes() throws Exception {
//This is Camel 3.0 API with RouteReifier
RouteReifier.adviceWith(defaultContext.getRouteDefinition("hello_route"), defaultContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
//Hook into the current route, intercept log endpoints and reroute them to mock
interceptSendToEndpoint("log:foo").to("mock:foo");
interceptSendToEndpoint("log:bar").to("mock:bar");
}
});
}
}
警告未来的访客:此处的测试案例演示了如何使用log:
拦截mock:
端点并对其设定期望。测试用例可能没有进行任何有价值的测试。