testing camel quartz route

2019-05-20 21:34发布

问题:

I try to make a junit test for apache camel route. Something like this :

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(
    loader = CamelSpringDelegatingTestContextLoader.class
)
public class MyExportRouteBuilderIT extends CamelTestSupport {
    @Test
    public void test() {
        // trigger and check the files made by route builder processor
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new MyExportRouteBuilder();
    }
}

The builder class is defined like this

from("quartz2://exportJob?cron=" + cronTrigger)
.setHeader(FILE_NAME, expression(FILE_NAME_FORMAT))
.process(myExportRouteProcessor)
.marshal(new BindyCsvDataFormat(MyExportData.class))
.to("file:///destination);

The 'myExportRouteProcessor' class just gets some data from the JPA repository and puts the results to the route. What I want is to trigger this route in the test class to check if the whole process was properly finished. Currently, processor is not fired. What should I do more ?

回答1:

You can replace quartz2 component in your test with direct using AdviceWithRouteBuilder#replaceFromWith.

@Test
public void test() throws Exception{
   //mock input route (replace quartz with direct)
   context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
       @Override
       public void configure() throws Exception {
           replaceFromWith("direct:triggerQuartz");
       }
   });

   //trigger endpoint
   sendBody("direct:triggerQuartz", null);

   //do some assertions
}