gpt4 book ai didi

java - 异常处理程序的 Camel 建议

转载 作者:行者123 更新时间:2023-12-02 11:06:07 25 4
gpt4 key购买 nike

我需要测试我的 Camel 异常处理程序:

@Configuration
public class RouteConfiguration extends RouteBuilder {

@Override
public void configure() throws Exception {

onException(HttpOperationFailedException.class).
handled(true).
log("HttpOperationFailedException: ${exception}").
onExceptionOccurred(myRestExceptionProcessor).id("myRestExceptionProcessor").end();

from("direct:myPrettyRoute").routeId("myPrettyRoute");//lots of routing here

}
}

我试图在 myRestExceptionProcessor 之后添加adviceWith,但找不到方法。

public class MyExceptionRoutingTest {

@Autowired
private CamelContext context;

@Before
public void before() throws Exception {
if (ServiceStatus.Stopped.equals(context.getStatus())) {
log.info("prepare mocks endpoint");

List<OnExceptionDefinition> ed = context.getErrorHandlerBuilder().getErrorHandlers(context.getRoutes().get(0).getRouteContext());
//FAILS, because context.getRoutes() is empty at the moment
//even if it wasn't, getErrorHandlerBuilder() is deprecated
}
}
}

我需要为异常处理程序定义添加类似的内容:

    .adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("myExceptionProcessor").after().to(myResultEndpoint).id("myResponseEndpoint");
}
});

可能吗?

最佳答案

我不完全理解你是否想测试你的错误处理程序(onException block )或只是你的myRestExceptionProcessor,但从Camel的角度来看,这是两种测试:

  1. 路由测试用于测试您的路由逻辑并确保在路由中可能发生的各种情况下消息正确路由。这是您使用 Camel Testkit 编写的测试类型(提供 AdviceWith 等)。
  2. 经典单元测试,用于测试隔离的 Bean、处理器或路由中用于实现业务逻辑的任何其他内容。这种测试是用JUnit、TestNG或者其他经典的单元测试框架来完成的,与Camel无关。 不要尝试使用 Camel Route 测试来测试此类组件,因为它比单元测试复杂得多!

因此,如果您想在发生错误时测试路由,您可以在路由测试中抛出所需的错误以触发错误处理程序。如果您使用像 Spring 这样的依赖注入(inject)框架,这很容易,因为您可以注入(inject)一个抛出错误的测试 Bean,而不是路由中使用的真实 Bean。

要在路由末尾添加模拟端点,请使用 adviceWith

.adviceWith(camelContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveAddLast().to("mock:error");
}
}

希望这能有所帮助。请随意扩展您的问题以更详细地阐述您的问题。

关于java - 异常处理程序的 Camel 建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50952175/

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