jbwt
jbwt

Reputation: 394

How do i extract the message body from a mockendpoint in a java camel test?

This is a question on Java, camel. I have a route, in which I am trying to extract the message body from the vm:region endpoint, but get an ArrayIndexOutOfBounds when i try to access the first index of the received exchanges, even though the expectedMessageCount of 1 is asserted. My route and code is shown below.

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

My camel test in eclipse is as follows:

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

Any assistance would be greatly appreciated.

Upvotes: 3

Views: 8246

Answers (2)

fermin.saez
fermin.saez

Reputation: 164

(I would have commented, but don't have enough points either) Your assertion is satisfied because it does receive a message. What seems to be giving you trouble is that the body of the message is null, and not the message itself.

Upvotes: 1

Steve Harrington
Steve Harrington

Reputation: 942

I might have made this a "comment" rather than an "answer" but I don't have sufficient reputation points on stack overflow. At any rate, conceptually there is not a problem (see my code below for a similar usage, which works in my tests). There must be some other glitch; have you stepped through it in a debugger?

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

Upvotes: 5

Related Questions