我是 apache camel 的新手,所以我仍在努力编写骆驼测试用例。
我定义了以下路线
from("direct:routeToTest")
.id(ROUTE_ID)
.to(LOOK_UP_ROUTE)
.choice()
.when(some-condition)
.choice()
.when(condition)
.to(CREATE_ROUTE)
.otherwise().process(exchange -> exchange.getIn().setBody(prepareResponse(""))
.endChoice()
.otherwise()
.log("Some Issue")
.process(exchange -> unknownError(exchange))
.endChoice();
}
在测试时,我试图拦截在我的路由中定义并对其设置一些模拟响应。因此,经过一番搜索,我发现使用adviceWith是实现它的正确方法。
所以我的测试如下。测试的结果是,它仍然会去 Look_up_route(直接:lookUpRoute,另一个定义的路由)处理传递的数据,但期望代码应该跳过这个并将响应设置为“MockResponse”
@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@RunWith(CamelSpringBootRunner.class)
@UseAdviceWith
@MockEndpoints
@DisableJmx(false)
public class RouteTest {
@Autowired
private ProducerTemplate producerTemplate;
@Autowired
private CamelContext context;
@Test
public void testResponseToJSON() throws Exception {
SomeObject someObject = getObject();
context.getRouteDefinition(ROUTE_ID).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
interceptSendToEndpoint(LOOK_UP_ROUTE)
.skipSendToOriginalEndpoint()
.transform("MockOutput");
}
);
context.start();
Object object = producerTemplate.requestBody(direct:routeToTest, someObject);
}
}
我想知道如何跳过 .to(LOOK_UP_ROUTE) 并将 mockResponse 设置为它。