Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 针对ExceptionHandler的Camel adviceWith_Java_Junit_Apache Camel - Fatal编程技术网

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的角度来看,这是两种测试:

  • 路由测试测试您的路由逻辑,并确保在路由中可能发生的各种情况下正确路由消息。这是您使用Camel Testkit编写的测试类型(它提供adviceWith和更多)
  • 经典的单元测试用于测试一个独立的Bean、处理器或在实现业务逻辑的过程中使用的任何其他东西。这种测试是用JUnit、TestNG或其他经典的单元测试框架完成的,与Camel无关不要尝试使用驼峰路线测试来测试此类组件,因为它比单元测试复杂得多 因此,如果要在发生错误时测试路由,请在路由测试中抛出所需的错误以触发错误处理程序。如果您使用像Spring这样的依赖注入框架,这很容易,因为您可以注入抛出错误的测试Bean,而不是路由中使用的真正Bean

    要在管线末端添加模拟端点,请使用


    希望这有点帮助。请随意扩展您的问题,进一步阐述您的问题

    我不完全理解您是想测试错误处理程序(
    OneException
    块)还是只测试
    myRestExceptionProcessor
    ,但从Camel的角度来看,这是两种测试:

  • 路由测试测试您的路由逻辑,并确保在路由中可能发生的各种情况下正确路由消息。这是您使用Camel Testkit编写的测试类型(它提供adviceWith和更多)
  • 经典的单元测试用于测试一个独立的Bean、处理器或在实现业务逻辑的过程中使用的任何其他东西。这种测试是用JUnit、TestNG或其他经典的单元测试框架完成的,与Camel无关不要尝试使用驼峰路线测试来测试此类组件,因为它比单元测试复杂得多 因此,如果要在发生错误时测试路由,请在路由测试中抛出所需的错误以触发错误处理程序。如果您使用像Spring这样的依赖注入框架,这很容易,因为您可以注入抛出错误的测试Bean,而不是路由中使用的真正Bean

    要在管线末端添加模拟端点,请使用


    希望这有点帮助。请随意扩展您的问题,进一步阐述您的问题

    在不改变路线的情况下,我解决了以下问题:

    //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