Java 针对ExceptionHandler的Camel adviceWith
我需要测试骆驼异常处理程序:Java 针对ExceptionHandler的Camel adviceWith,java,junit,apache-camel,Java,Junit,Apache Camel,我需要测试骆驼异常处理程序: @Configuration public class RouteConfiguration extends RouteBuilder { @Override public void configure() throws Exception { onException(HttpOperationFailedException.class). handled(true).
@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
}
}
}
有可能吗?我不完全理解您是想测试错误处理程序(
OneException
块)还是只测试myRestExceptionProcessor
,但从Camel的角度来看,这是两种测试:
希望这有点帮助。请随意扩展您的问题,进一步阐述您的问题 我不完全理解您是想测试错误处理程序(
OneException
块)还是只测试myRestExceptionProcessor
,但从Camel的角度来看,这是两种测试:
希望这有点帮助。请随意扩展您的问题,进一步阐述您的问题 在不改变路线的情况下,我解决了以下问题:
//entry point of the route is invoked here
Exchange send = myProducer.withBody("body is here").send();
HttpOperationFailedException exception = send.getException(HttpOperationFailedException.class);
String responseBody = exception.getResponseBody();
//recieved result and made assertions
assert responseBody != null; // any other assertions
在不改变路线的情况下,我解决了以下问题:
//entry point of the route is invoked here
Exchange send = myProducer.withBody("body is here").send();
HttpOperationFailedException exception = send.getException(HttpOperationFailedException.class);
String responseBody = exception.getResponseBody();
//recieved result and made assertions
assert responseBody != null; // any other assertions
4年前我就有了一个解决方案,到目前为止还没有找到一个有效的解决方案。根据Camel开发人员的说法,他们认为业务逻辑(也就是说要测试的东西)不应该在异常处理程序中处理。如果要在异常处理程序中维护一个
to(“某个组件:…”)
,则可以尝试使用interceptSendToEndpoint(“某个组件:”).skipSendToOriginalEndpoint().to(“模拟:错误”)截取该异常
though@RomanVottner,很遗憾,我在那里没有端点。只有一些处理和封送。我看到了一个解决方法:add.to(direct:myErrorhandlerFinish)在处理程序的末尾&创建一个相应的路由。对我来说,它看起来非常脏。根据他们修复了使用AdviceWith和在OneException上使用weave方法等。不起作用。
因此,此修复现在应该允许编织OneException块。这似乎在Camel 3I中再次被打破,我已经4年了,还没有找到一个到目前为止,这是一个有效的解决方案。根据Camel开发人员的说法,他们认为业务逻辑(也就是说要测试的东西)不应该在异常处理程序中处理。如果您要在异常处理程序中维护一个to(“某个组件:…”)
,您可以尝试使用interceptSendToEndpoint(“某个组件:”)来拦截它.skipSendToOriginalEndpoint().to(“模拟:错误”)
though@RomanVottner,很不幸,我在那里没有端点。只是一些处理和封送。我在处理程序的末尾看到了一个解决方法:add.to(direct:myErrorhandlerFinish)并创建相应的路由。对我来说,它看起来非常脏。根据他们的修复了使用AdviceWith和在OneException上使用weave方法等。不起作用。
因此,此修复现在应该允许编织OneException块。这似乎在Camel 3中再次被打破。完整的测试用例是:调用路由-模拟异常-确保捕获并处理它正确,检查结果。单独测试处理器太简略了。我在答案中添加了如何将某些内容附加到routeThanks,weaveAddLast()是有用的,但不用于接收来自errorhandler的响应。如果发生异常(mockResultEndpoint.Assertessatified())失败,也将跳过该测试。完整的测试用例是:要调用路由-模拟异常-确保捕获并正确处理它,请检查结果。单独测试处理器太简略了。我在答案中添加了如何将某些内容附加到routeThanks,weaveAddLast()是有用的,但不用于接收来自errorhandler的响应。如果出现异常(mockResultEndpoint.assertessatified())失败,也将跳过该步骤使用Java断言可能会误导测试,因为它们仅在应用程序启动时传递-ea
(启用断言)时才被激活。在缺少提供该选项时没有失败
//entry point of the route is invoked here
Exchange send = myProducer.withBody("body is here").send();
HttpOperationFailedException exception = send.getException(HttpOperationFailedException.class);
String responseBody = exception.getResponseBody();
//recieved result and made assertions
assert responseBody != null; // any other assertions