简体   繁体   English

骆驼的adviceWith for ExceptionHandler

[英]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 ,但是从骆驼的角度来看,这是两种测试:

  1. Routing-Tests to test your routing logic and make sure that messages are correctly routed under various conditions that could happen in the route. 路由测试 -测试您的路由逻辑 ,并确保在路由中可能发生的各种情况下正确路由消息 This is the kind of tests you write with the Camel Testkit (that offers adviceWith and much more). 这就是您使用Camel Testkit编写的测试类型(它提供advisorWith等)。
  2. Classic unit tests to test an isolated Bean, Processor or anything else that is used in the route to implement business logic . 经典单元测试可测试隔离的Bean,处理器或在实现业务逻辑的路径中使用的其他任何东西。 This kind of test is done with JUnit, TestNG or other classic unit test frameworks, it has nothing to do with Camel. 这种测试是通过JUnit,TestNG或其他经典的单元测试框架完成的,与Camel无关。 Do not try to test such components with Camel Route tests since it is much more complicated than in a unit test! 不要尝试使用Camel Route测试来测试此类组件,因为它比单元测试要复杂得多!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM