我的服务中有以下路线:
public void configure() {
/*
* Scheduled Camel route to produce a monthly report from the audit table.
* This is scheduled to run the first day of every month.
*/
// @formatter:off
from(reportUri)
.routeId("monthly-report-route")
.log("Audit report processing started...")
.to("mybatis:updateProcessControl?statementType=Update")
.choice()
/*
* If the rows updated is 1, this service instance wins and can run the report.
* If the rows updated is zero, go to sleep and wait for the next scheduled run.
*/
.when(header("CamelMyBatisResult").isEqualTo(1))
.process(reportDateProcessor)
.to("mybatis:selectReport?statementType=SelectList&consumer.routeEmptyResultSet=true")
.process(new ReportProcessor())
.to("smtp://smtpin.tilg.com?to="
+ emailToAddr
+ "&from=" + emailFromAddr )
.id("RecipientList_ReportEmail")
.endChoice()
.end();
// @formatter:on
}
当我尝试对此进行测试时,它给了我一个错误,指出骆驼无法自动创建组件 mybatis。我对测试骆驼路线没有经验,所以我不完全确定该去哪里。第一个 mybatis 调用更新了表中的一行,该行不在测试中,所以我想做一些事情,比如当端点被命中时,返回值为 1 的 CamelMyBatisResult 标头。第二个 mybatis 端点应该返回一个 hashmap(第一次测试为空,第二次填充)。我如何通过骆驼测试实施一种何时/然后的机制?我查看了模拟端点骆驼文档,但我不知道如何应用它并让它返回一个值到交换,然后继续路由(测试的最终结果是检查一封电子邮件或不发送附件)
编辑:尝试同时使用 replace().set* 方法并通过调用内联处理器替换 mybatis 端点:
@Test
public void test_reportRoute_NoResultsFound_EmailSent() throws Exception {
List<AuditLog> bodyList = new ArrayList<>();
context.getRouteDefinition("monthly-report-route").adviceWith(context,
new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceFromWith(TEST);
weaveById("updateProcControl").replace()
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader("CamelMyBatisResult", 1);
}
});
weaveById("selectReport").replace()
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(bodyList);
}
});
weaveById("RecipientList_reportEmail").replace()
.to("smtp://localhost:8083"
+"?to=" + "test@test.com"
+"&from=" + "test1@test1.com");
}
});
ProducerTemplate prod = context.createProducerTemplate();
prod.send(TEST, exch);
assertThat(exch.getIn().getHeader("CamelMyBatisResult"), is(1));
assertThat(exch.getIn().getBody(), is(""));
}
到目前为止,标题仍然为空,正文也是如此(TEST 变量是直接组件)