You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Larry Meadors <la...@gmail.com> on 2012/09/09 18:35:54 UTC

How to mock a bean in a route for testing

I have this route:

<route id="circRoute">
	<from uri="{{circ.start}}"/>
	<unmarshal ref="xs"/>
	<bean ref="circService"/>
	<marshal ref="xs"/>
	<to uri="{{circ.end}}"/>
</route>

How do I mock the "circService" component in a test? I've been looking
for hours and can't seem to find any example of this or make sense of
anything in the book or the docs.

Larry

Re: How to mock a bean in a route for testing

Posted by Larry Meadors <la...@gmail.com>.
Thanks, here's what I ended up doing.

Changed the bean call to look like this:
<to uri="{{circ.service}}"/>

That value is defined in a properties file - in live use it's
"bean:circService", but under testing it's "mock:circService".

In my test, I added this:

@EndpointInject(uri = "{{circ.service}}")
protected MockEndpoint circServiceMock;

Then in my test case, I added this:

circServiceMock.returnReplyBody(new Expression() {
@Override
public <T> T evaluate(Exchange exchange, Class<T> type) {
 return exchange.getContext().getTypeConverter().convertTo(type,
circResponse);
}
});

The circResponse variable has what I want the mocked service to return.

Larry

Re: How to mock a bean in a route for testing

Posted by Christian Müller <ch...@gmail.com>.
I would use
<to uri="bean://circService"/>
instead of
<bean ref="circService"/>
but may be this is not necessary.

Than, in your unit test, use

    @Test
    public void test() throws Exception {
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                interceptSendToEndpoint("bean://circService")
                    .skipSendToOriginalEndpoint()
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws
Exception {
                            exchange.getIn().setBody("dummy");
                        }
                    })
            }
        });

        // your real test commes here
    }

or

    @Test
    public void test() throws Exception {
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                interceptSendToEndpoint("bean://circService")
                    .skipSendToOriginalEndpoint()
                    .to("bean://circServiceMock");
            }
        });

        // your real test commes here
    }

Best,
Christian


On Sun, Sep 9, 2012 at 6:35 PM, Larry Meadors <la...@gmail.com>wrote:

> I have this route:
>
> <route id="circRoute">
>         <from uri="{{circ.start}}"/>
>         <unmarshal ref="xs"/>
>         <bean ref="circService"/>
>         <marshal ref="xs"/>
>         <to uri="{{circ.end}}"/>
> </route>
>
> How do I mock the "circService" component in a test? I've been looking
> for hours and can't seem to find any example of this or make sense of
> anything in the book or the docs.
>
> Larry
>



--