[英]Camel adviceWith for ExceptionHandler
I need to test my Camel Exception handler: 我需要测试我的骆驼异常处理程序:
@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
}
}
I'm trying to add adviceWith after myRestExceptionProcessor, but can't find a way. 我正在尝试在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
}
}
}
I need to add something like this for the exceptionHandler definition: 我需要为exceptionHandler定义添加如下内容:
.adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("myExceptionProcessor").after().to(myResultEndpoint).id("myResponseEndpoint");
}
});
Is it possible? 可能吗?
I don't fully understand if you want to test your error handler ( onException
block) or just your myRestExceptionProcessor
, but from a Camel perspective these are two kinds of tests: 我不完全了解您是要测试错误处理程序(
onException
块)还是仅myRestExceptionProcessor
,但是从骆驼的角度来看,这是两种测试:
So, if you want to test your routing when an error occurs you throw the needed error in your route test to trigger the error handler. 因此,如果要在发生错误时测试路由,则会在路由测试中抛出所需的错误以触发错误处理程序。 If you use a dependency injection framework like Spring this is easy since you can inject a test Bean that throws an error instead of a real Bean used in the route.
如果使用像Spring这样的依赖注入框架,这很容易,因为您可以注入引发错误的测试Bean,而不是路由中使用的实际Bean。
To add a Mock endpoint at the end of a route, use adviceWith 要在路线末端添加Mock端点,请使用adviceWith
.adviceWith(camelContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveAddLast().to("mock:error");
}
}
Hope this helps a bit. 希望这个对你有帮助。 Feel free to extend your question to elaborate your problem a bit more.
随意扩展您的问题,以进一步阐述您的问题。
I've solved the trick as follows, without changing the route: 我已经解决了以下技巧,而没有更改路线:
//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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.