snowindy
snowindy

Reputation: 3251

Apache Camel: how to get a correct mock endpoint for beanRef?

I have a route like this

from("direct:start").beanRef("someBean");

For a unit test I try to get mock endpoint for it, but expectedMessageCount condition is not satisfied.

MockEndpoint beanMock = getMockEndpoint("mock:bean:someBean");
beanMock.expectedMessageCount(1);

If I change my route into this, everything works fine.

from("direct:start").to("bean:someBean");

The folowing does not work either:

MockEndpoint beanMock = getMockEndpoint("mock:ref:someBean");

How to get a correct mock endpoint for beanRef?

Upvotes: 2

Views: 9804

Answers (1)

dursun
dursun

Reputation: 1846

The problem is that beanRef does not produce an endpoint, so you can not access it by getMockEndPoint. if you want to test the result of your bean, you can add a mock endPoint and test its content.

here is an example:

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class BeanRefMock extends CamelTestSupport {
    public static class SomeBean{
        public void handle(Exchange ex){
            System.out.println("SomeBean : " +ex);
            ex.getIn().setBody(ex.getIn().getBody() +" is Processed By Bean");
        }
    }
    @Override
    protected CamelContext createCamelContext() throws Exception {
        CamelContext camelContext = super.createCamelContext();
        return camelContext;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.camel.test.junit4.CamelTestSupport#createRouteBuilder()
     */
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start").bean(SomeBean.class).to("mock:postBean");
            }
        };
    }

    @Test
    public void testQuoteCount() throws Exception {
        MockEndpoint mockEndpoint = getMockEndpoint("mock:postBean");
        mockEndpoint.expectedBodiesReceived("hello mock is Processed By Bean");

        template.sendBody("direct:start", "hello mock");
        mockEndpoint.assertIsSatisfied();
    }

}

Upvotes: 1

Related Questions