java - 如何从 java camel 测试中的模拟端点提取消息正文?

标签 java apache-camel

这是一道关于Java的题, Camel 。我有一条路线,其中我试图从 vm:region 端点提取消息正文,但是当我尝试访问接收到的交换的第一个索引时得到 ArrayIndexOutOfBounds,即使断言 1 的 expectedMessageCount 也是如此。我的路线和代码如下所示。

        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");
    }

我在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....
    }

如有任何帮助,我们将不胜感激。

最佳答案

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

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

关于java - 如何从 java camel 测试中的模拟端点提取消息正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22900951/

相关文章:

java - 线程中的异常 "main"java.lang.IndexOutOfBoundsException : Index: 0, 大小:0 错误

java - 基于 TableView 自动更改 TableModel?

java - 在多个实例上暂停、恢复和停止线程和多线程的现代方法?

java - 如何使用模拟端点在 Camel 测试中启动路由

error-handling - 在 Apache Camel 中重新交付用尽后如何调用 Web 服务?

java - 如何理解 Java 中的 "Happens-before consistency is not sufficient"

java - 执行 CVS checkout 后 Ant 脚本无法找到目标

java - 是否可以使用 java 接口(interface)或 bean 启动 Camel 路线?

apache-camel - Apache Camel 使用 HTTP 定义路由

java - Apache Camel 中的 header 谓词