如何从java camel测试中的mockendpoint中提取消息体?

时间:2014-04-06 22:45:55

标签: java apache-camel

这是一个关于Java的问题,骆驼。我有一个路由,我试图从vm:region端点提取消息体,但是当我尝试访问接收到的交换的第一个索引时获取一个ArrayIndexOutOfBounds,即使expectMessageCount为1也是如此。我的路线和代码如下所示。

        from(uriMap.get("start_cDirect_2")).routeId("start_cDirect_2")
                .to(uriMap.get("cLog_2"))

                .id("cLog_2").choice().id("cMessageRouter_1").when()
                .simple("${in.header.type} == 'region'")

                .to(uriMap.get("vm:region_cMessagingEndpoint_2"))
                .id("cMessagingEndpoint_2").otherwise()

                .to(uriMap.get("vm:zipcode_cMessagingEndpoint_3"))
                .id("cMessagingEndpoint_3");
        from(uriMap.get("vm:start_cMessagingEndpoint_1"))
                .routeId("vm:start_cMessagingEndpoint_1")
                .to(uriMap.get("cLog_1"))

                .id("cLog_1").beanRef("beans.bean1").id("cBean_1")
                .to(uriMap.get("start_cDirect_2")).id("cDirect_1");
    }

我在日食中的骆驼测试如下:

public class ShowUnitTestTest extends CamelTestSupport{
    @EndpointInject(uri = "mock:vm:region")
    protected MockEndpoint resultEndpoint;
    @Produce(uri = "vm:start")
    protected ProducerTemplate template; 
    @Override
    public String isMockEndpoints() {
        return "*";
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        ShowUnitTest route = new ShowUnitTest();
        route.initUriMap();
        return route;
    }

    @Test
    public void testRegionRouting() throws Exception {
        MockEndpoint regionMock = getMockEndpoint("mock:vm:region");
        MockEndpoint zipcodeMock = getMockEndpoint("mock:vm:zipcode");

        regionMock.setExpectedMessageCount(1);
        zipcodeMock.setExpectedMessageCount(0);

        // send a message with the region header
        sendBody("mock:log:cLog_1", "foo");
       template.sendBodyAndHeader("vm:start", "Foobar", "type", "region");

        // check the assertion
        assertMockEndpointsSatisfied();        
        Exchange exchange = regionMock.getExchanges().get(0);
        Message in = exchange.getIn();
        //try and print out the message body....
    }

非常感谢任何协助。

2 个答案:

答案 0 :(得分:3)

我可能已经把它作为“评论”而不是“回答”,但我没有足够的声誉点在堆栈溢出。无论如何,概念上没有问题(请参阅下面的代码,了解类似的用法,在我的测试中有效)。一定有其他一些小故障;你有没有在调试器中逐步完成它?

// here the variable resultEndpointFtpCitationImages is a MockEndpoint in my junit test
byte[] bytesReceivedViaFtp = (byte[]) resultEndpointFtpCitationImages.getExchanges().get(0).getIn().getBody();

答案 1 :(得分:1)

(我会评论,但也没有足够的分数)你的断言是满意的,因为它确实收到了一条消息。似乎给你带来麻烦的是消息正文为空,而不是消息本身。