You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Debraj Manna <su...@gmail.com> on 2016/03/04 08:01:37 UTC

Doing camel-test with a bean in the route

Hi,

This is my first post in the camel user group. Some how my first post did
not go through the mailing list. I hope some one can point to me the right
direction.

I am trying to write a test for a camel app which looks something like
below:-

public class OrderMainApp {
    public static void main(String... args) throws Exception {
    OrderMainApp orderMainApp = new OrderMainApp();
    DefaultCamelContext camelContext = new DefaultCamelContext();
    ProducerTemplate producer = camelContext.createProducerTemplate();
    camelContext.setRegistry(orderMainApp.createRegistry(producer));
    camelContext.addRoutes(new OrderRouteBuilder(producer));
    camelContext.start();
    }

    protected JndiRegistry createRegistry(ProducerTemplate producer) throws
Exception {
    JndiRegistry jndi = new JndiRegistry();
    OrderHelper orderHelper = new OrderHelper();
    orderHelper.setProducer(producer);
    jndi.bind("orderHelper", orderHelper);
    return jndi;
    }
}

In OrderRouteBuilder configure has routes like below:-

//processor is a custom JSONProcessor extending Processor
from("jetty:http://localhost:8888/orchestratorservice").process(processor);
from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders");


My test class look something like below:-

public class OrderMainAppTest extends CamelTestSupport {

    @Produce(uri = "direct:getMarketplaceOrders")
    protected ProducerTemplate template;

    @EndpointInject(uri = "bean:orderHelper?method=getMarketplaceOrders")
    protected MockEndpoint resultEndpoint;

    @Test
    public void testSendMatchingMessage() throws Exception {
    String expectedBody = "<matched/>";

    template.sendBody("{\"fromDateTime\": \"2016-01-11 10:12:13\"}");

    resultEndpoint.expectedBodiesReceived(expectedBody);

    resultEndpoint.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
        @Override
        public void configure() {

from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders");

        }
    };
    }
}

Whenever I am running the test I am getting the below exception:-

java.lang.IllegalArgumentException: Invalid type:
org.apache.camel.component.mock.MockEndpoint which cannot be injected via
@EndpointInject/@Produce for:
Endpoint[bean://orderHelper?method=getMarketplaceOrders]

Looking at the exception it seems it is not possible to use the OrderHelper
bean in mock endpoint. Can some one let me know how can I test my camel
application which has a bean in the route?

Thanks,
D