Java 在测试期间禁用窃听
我希望在测试时能够阻止我的路由通过窃听发送信息。这是我目前用于使路由可测试的方法Java 在测试期间禁用窃听,java,spring-boot,testing,apache-camel,Java,Spring Boot,Testing,Apache Camel,我希望在测试时能够阻止我的路由通过窃听发送信息。这是我目前用于使路由可测试的方法 private void setupRoute(String routeId, String inputEndpoint, String outputEndpoint) throws Exception { camelContext.getRouteDefinition(routeId) .autoStartup(true) .advi
private void setupRoute(String routeId, String inputEndpoint, String outputEndpoint) throws Exception {
camelContext.getRouteDefinition(routeId)
.autoStartup(true)
.adviceWith(camelContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith(inputEndpoint);
interceptSendToEndpoint(outputEndpoint)
.skipSendToOriginalEndpoint()
.to(MOCK_OUTPUT);
//Should stop the route from wiretapping the messages to the archiving routes.
if (camelContext.hasEndpoint("direct:archiver") != null) {
interceptSendToEndpoint("direct:archiver")
.skipSendToOriginalEndpoint().stop();
}
if (camelContext.hasEndpoint("direct:elasticSearch") != null) {
interceptSendToEndpoint("direct:elasticSearch")
.skipSendToOriginalEndpoint().stop();
}
}
});
我认为,通过检查这些路由的上下文,然后禁用它们(如果它们存在),可以解决问题。但在调试时,我注意到这两个路由都没有保存为camelContext下的端点,因此这不起作用
在测试过程中禁用Wiratap的正确方法是什么?好吧,我自己已经弄明白了。只需将其添加到AdviceWithRouteBuilder中:
interceptSendToEndpoint("direct:archiver")
.skipSendToOriginalEndpoint().stop();
interceptSendToEndpoint("direct:elasticSearch")
.skipSendToOriginalEndpoint().stop();
在我最初的问题中,我在截取这些端点之前检查了它们是否存在,但这显然不是必需的。如果您试图截取的端点不存在,它将什么也不做。好吧,我自己已经找到了答案。只需将其添加到AdviceWithRouteBuilder中:
interceptSendToEndpoint("direct:archiver")
.skipSendToOriginalEndpoint().stop();
interceptSendToEndpoint("direct:elasticSearch")
.skipSendToOriginalEndpoint().stop();
在我最初的问题中,我在截取这些端点之前检查了它们是否存在,但这显然不是必需的。如果您试图拦截的端点不存在,它将什么也不做