骆驼:如何模拟具有两个端点的路线
Camel: How to mock a route with two endpoints
我是 Camel 的新手,我需要了解如何对具有两个端点的路由进行单元测试。第一个端点获取用户 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)
}
};
}
所以我必须在我的测试 class 中同时模拟 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();
}
这里有一个link to the Unit Test cases for the Mock component. It shows how to implement tests with mock:
endpoints and CamelTestSupport
. @Roman Vottner他的评论完全正确。
This test case may be of specific interest to you since it shows how to swap an smtp:
endpoint with a mock:
endpoint. Additionally, here 是关于如何模拟现有端点(像测试探针一样使用它们)的官方文档。
警告:请记住,Camel 3.0 API 在该地区与 Camel 2.x API 完全不同。祝你好运!
我今天有时间快速破解一些演示代码,使用 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");
}
});
}
}
警告未来的访问者: 这里的测试用例演示了如何使用 mock:
拦截 log:
端点并对其设置期望。测试用例可能没有测试任何有价值的东西。
我是 Camel 的新手,我需要了解如何对具有两个端点的路由进行单元测试。第一个端点获取用户 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)
}
};
}
所以我必须在我的测试 class 中同时模拟 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();
}
这里有一个link to the Unit Test cases for the Mock component. It shows how to implement tests with mock:
endpoints and CamelTestSupport
. @Roman Vottner他的评论完全正确。
This test case may be of specific interest to you since it shows how to swap an smtp:
endpoint with a mock:
endpoint. Additionally, here 是关于如何模拟现有端点(像测试探针一样使用它们)的官方文档。
警告:请记住,Camel 3.0 API 在该地区与 Camel 2.x API 完全不同。祝你好运!
我今天有时间快速破解一些演示代码,使用 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");
}
});
}
}
警告未来的访问者: 这里的测试用例演示了如何使用 mock:
拦截 log:
端点并对其设置期望。测试用例可能没有测试任何有价值的东西。