如何在 java 骆驼测试中从模拟端点提取消息正文?

jbw*_*bwt 3 java apache-camel

这是一个关于Java、camel的问题。我有一条路线,其中我尝试从 vm:region 端点提取消息正文,但当我尝试访问接收到的交换的第一个索引时,得到一个 ArrayIndexOutOfBounds,即使预期的 MessageCount 为 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");
    }
Run Code Online (Sandbox Code Playgroud)

我在eclipse中的camel测试如下:

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....
    }
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。

Ste*_*ton 5

我可能将此作为“评论”而不是“答案”,但我在堆栈溢出上没有足够的声誉点。无论如何,从概念上讲没有问题(请参阅下面的代码以了解类似的用法,该用法在我的测试中有效)。一定还有其他故障;你在调试器中单步调试过它吗?

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