使用MockEndpoint测试骆驼路由 [英] Testing Camel routes with MockEndpoint

查看:0
本文介绍了使用MockEndpoint测试骆驼路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么此测试失败,并显示"java.lang.AssertionError:mock://Destino Receiven Message Count.Expect:<;12>但is:<;0>"?我只是想测试Camel是否真的可以对消息进行重新排序。

导入:

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.guice.CamelModuleWithRouteTypes;
import org.jukito.JukitoModule;
import org.jukito.JukitoRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Inject;

测试:

@RunWith(JukitoRunner.class) public class ResequenceTest {

    @Inject protected CamelContext context;
    @Produce protected ProducerTemplate template;

    public static class Module extends JukitoModule {
        @SuppressWarnings("unchecked") protected void configureTest() {
            install(new CamelModuleWithRouteTypes(OrderingTestRouteBuilder.class));
        }
    }

    @Test public void testDozenMsgsOrderByIntegerBody() throws Exception {
        // fail();
            Integer[] input = new Integer[] {12, 11, 10, 9, 8, 7, 6, 5, 1, 2, 3, 4};
            List<Integer> expectedOutput = new ArrayList<Integer>();
        for (int i=1; i<=12; i++) {expectedOutput.add(i);};
        for (Integer i : input) {template.sendBody("mock:receptor", i);};
        MockEndpoint resultEndpoint = context.getEndpoint("mock:destino", MockEndpoint.class);
        resultEndpoint.expectedBodiesReceived(expectedOutput);
        resultEndpoint.assertIsSatisfied();
    }

}

路线:

class OrderingTestRouteBuilder extends RouteBuilder {
        @Override public void configure() throws Exception {
        from("direct:start")
            .to("mock:receptor")
            .resequence(body(Integer.class))
            .to("mock:destino");
    }
}

谢谢, 鲁道夫

推荐答案

在向Camel发送消息之前,应在模拟上设置预期。具体步骤为

  1. 设置模拟的预期
  2. 发送消息
  3. 断言

这篇关于使用MockEndpoint测试骆驼路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆