如何在 Apache Camel 中模拟 cxfbean 调用?

How to mock cxfbean call in Apache Camel?

我有调用 cxfbean 的路由:

.to("cxfbean:reservationService")

在我的测试中尝试用

模拟这个
@EndpointInject(uri = "mock:reservationService")
MockEndpoint reservationSystemMock;

@BeforeMethod
private void setUpContext() throws Exception
{
     context.getRouteDefinition( "send.to.res.svc.endpoint" ).adviceWith(
         context, new AdviceWithRouteBuilder() {
         @Override
         public void configure() throws Exception         
         {
              interceptSendToEndpoint("cxfbean:reservationService") 
                                      .skipSendToOriginalEndpoint()
                                      .to("mock:reservationService");
         }
     });
}

还尝试使用 weaveByToString( "**reservationService" ).replace().to( "mock:reservationService" ); 进行模拟。在这两种情况下,我得到:

Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: reservationService

我想在没有 cxf bean 实例化的情况下测试我的路由。我正在使用 CamelTestSupport class 作为 parent.

记得在测试 class 时打开 advice-with。如果您使用这些注释,则将 @UseAdviceWith 添加到 class.

然后在您建议

之后启动骆驼上下文

设法使用 weaveByToString( "To[cxfbean:reservationService]" ) 模拟 cxfbean 端点:

@EndpointInject(uri = "mock:reservationService")
protected MockEndpoint reservationSystemMock;

@BeforeMethod
private void setUpContext() throws Exception
{
    context.getRouteDefinition( "send.to.res.svc.endpoint" ).adviceWith(
        context, new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {
                weaveByToString( "To[cxfbean:reservationService]" )
                .replace().to( "mock:reservationService" );
            }
        });
}

似乎我们也可以在调试观察器

中使用context.getRouteDefinitions().get(0).toString()查看weaveByToString的必要表达式