简体   繁体   中英

Camel: Mock a processor to test Route

I am trying to create a unit test for the routing. I have the following route configuration

from ("direct:getA")
    .routeId("get-a").startupOrder(1)
    .process(exchange -> {
        QueryObject queryObject = exchange.getIn().getBody(QueryObject.class);
        exchange.getIn().setHeader(foo, queryObject.getH());
        exchange.setOut(exchange.getIn());
    })
    .choice()
        .when(header(foo).isEqualTo(fooConstant.bar))
            .process("barProcessor")
        .when(header(foo).isEqualTo(fooConstant.bie))
            .process("bieProcessor")
    .end();

My question is, how can I mock "barProcessor" and "bieProcessor"?

I tried to use adviceWith but I could not retrieve the routeDefinition. The context.getRouteDefinitions() returns an empty list.

Edit: Below is the code snippet from my test.

RouteDefinition routeDef = context.getRouteDefinition("get-a");
routeDef.adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("*barProcessor*").process(
                    new Processor() {
                        @Override
                        public void process(Exchange exchange) {
                            System.out.println("bar");
                        }
                    }
            );
            interceptSendToEndpoint("*bieProcessor*").process(
                    new Processor() {
                        @Override
                        public void process(Exchange exchange) {
                            System.out.println("Bie");
                        }
                    }
            );
        }
    });

String request = <JSON Request>;
websocket.sendTest(request);

But the context.getRouteDefinition("get-a"); is returning null. And when I also used context.getRouteDefinitions() , it returns an empty list.

I found the cause of this issue. I forgot to Override the createRouteBuilder and createJndiContext.

@Override
protected RouteBuilder createRouteBuilder() {
    return new SampleRoute(<param>);
}

@Override
protected JndiContext createJndiContext() throws Exception {
    JndiContext context = new JndiContext();
    context.bind("barProcessor", new BarProcessor());
    context.bind("bieProcessor", new BieProcessor());
    return context;
}

I found the solution from this link: Unit Test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM