gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:20 25 4
gpt4 key购买 nike

这是一道关于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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com