Camel Test - 如何跳过处理器内ConsumerTemplate中使用的Endpoint

时间:2017-03-30 09:18:29

标签: unit-testing apache-camel

我实现了一个使用轮询使用者的服务。该模式与here非常相似(参见"基于计时器的轮询消费者")。

这是我的变体:

@Override
public void configure() throws Exception {
    from("quartz2://" + SCHEDULE + "?cron=" + checkNotNull(cron)).routeId(IN_ROUTE)
            .process(fetchMessages)
    ;
}

FetchMessages Processor:

@Override
public void process(Exchange exchange) throws Exception {
    while (true) {
        exchange = consumerTemplate.receive(MainRoute.ACTIVE_MQ + ":queue:" + "source", TIMEOUT);
        //no more messages within timeout
        if (exchange == null){
            break;
        }
        producerTemplate.send(MidRoute.MID_PLUG, exchange);
    }
}

我的问题是:如何在测试中跳过activemq端点?

到目前为止,我试图通过骆驼测试支持跳过它:

@Override
public String isMockEndpointsAndSkip() {
    return "(" + MainRoute.ACTIVE_MQ + ".*)|" + MidRoute.MID_PLUG;
}

并拦截:

@Before
public void prepare() throws Exception {
    context.getRouteDefinition(InRoute.IN_ROUTE).adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            replaceFromWith(in);
            interceptSendToEndpoint(MainRoute.ACTIVE_MQ + ":queue:" + "source")
                .skipSendToOriginalEndpoint()
                .to(amqMockSource);
        }
    });
}

两者都无济于事。仍然调用ActiveMq端点导致连接拒绝异常。

那么如何跳过bean中使用的端点?

1 个答案:

答案 0 :(得分:0)

你可以使用Camel中的编织机制来做到这一点。

  1. 首先使用.routeId(")为您的路由指定一个ID,然后将.process(“”)作为ID。所以写.process("fetchMessages").id("myId")

  2. 然后我们需要使用weave组件来删除处理器。

  3. 你的考试中有这样的东西。

       @Override
       public boolean isUseAdviceWith() {
            return true;
        }
    
     @Before
      public void setUp() throws Exception {
        super.setUp();
    
        context.getRouteDefinition("YourRouteId").adviceWith(context, new AdviceWithRouteBuilder()  {
          @Override
          public void configure() throws Exception {
            weaveById("myId").replace().setBody().constant("Some response"));
          }
        });    
    }
    

    基本上我们说我们想要使用AdviceWith,然后我们查看具有给定id的给定routeId的路由,并将其替换为某个响应体。这是您期望从activemq收到的响应。

相关问题