Camel Stub database interactions on Unit testing

367 views Asked by At

I am new to camel world. Using camel-cxf, i have implemented a webservice (Based on id on the request, query the database if found form the response with record details else return with static response) which has some very basic database interactions and a transformation.

I am trying to write some unit tests (independent of database i.e) stubbing out database interactions to form static response). Is there a way to skip sending to database endpoint and send the static data on exchange (mimicking database record) and validate the transformation result?

Regards, Shankar

1

There are 1 answers

0
Shankar On

I added the following code to my test class and validated the mockendpoints using expectedBodiesReceivedInAnyOrder method.

@Before
public void mockEndPoints() throws Exception{       
    AdviceWithRouteBuilder mockBldr = new AdviceWithRouteBuilder() {            
        @Override
        public void configure() throws Exception {              
            interceptSendToEndpoint("mybatis:queryTable?statementType=SelectList")              
            .skipSendToOriginalEndpoint()   
            .process(new Processor(){
                @Override
                public void process(Exchange exchange) throws Exception {
                    ArrayList<DataObject> ruleList = new ArrayList<DataObject>();
                        ruleList.add(new DataObject(1,"ABC"));                      
                        exchange.getIn().setBody(ruleList);
                    }               
            })
            .to("mock:mybatisSelect");                              
        }
    };
    context.getRouteDefinition("route1").adviceWith(context, mockBldr);
}