我是 apache Camel 的新手,所以我仍在努力编写 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();
}
在测试时,我试图拦截我的 route 定义的to,并为其设置一些模拟响应。因此,经过一番搜索,我发现使用 adviceWith 是实现它的正确方法。
所以我的测试如下。测试的结果是,它仍然会转到Look_up_route(direct: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设置为它。
最佳答案
您的intercept乍一看声明不错。但是,请查看此答案以获取替代方法。
你必须
- 将
id
添加到 route 的 LOOK_UP_ROUTE 步骤 - 然后您可以使用adviceWith删除或操作测试中标记的步骤
- 将消息正文(和 header )设置为测试所需的内容
关于java - Apache Camel | Spring 测试|拦截路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62335667/