我尝试对apache camel路线进行junit测试。 像这样:
@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();
}
}
构建器类的定义如下
from("quartz2://exportJob?cron=" + cronTrigger)
.setHeader(FILE_NAME, expression(FILE_NAME_FORMAT))
.process(myExportRouteProcessor)
.marshal(new BindyCsvDataFormat(MyExportData.class))
.to("file:///destination);
'myExportRouteProcessor'类只是从JPA存储库获取一些数据并将结果放入路径。 我想要的是在测试类中触发此路由以检查整个过程是否已正确完成。 目前,处理器未被解雇。我该怎么办?
答案 0 :(得分:2)
您可以直接使用AdviceWithRouteBuilder#replaceFromWith替换测试中的quartz2组件。
@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
}