gpt4 book ai didi

java - Camel : How to mock a route with two endpoints

转载 作者:行者123 更新时间:2023-12-02 01:17:33 24 4
gpt4 key购买 nike

我是 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)
}
};
}

所以我必须在我的测试类中模拟 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();
}

最佳答案

我今天有时间使用 Camel Spring boot 原型(prototype)快速破解一些演示代码。开始了。我的路线从计时器组件生成消息。不使用向端点的显式传递。

//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");
}
});
}

}

对 future 访问者的警告:此处的测试用例演示了如何使用 mock: 拦截 log: 端点并对其设置期望。测试用例可能没有测试任何有值(value)的东西。

关于java - Camel : How to mock a route with two endpoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58311414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com