【发布时间】:2017-03-31 23:19:28
【问题描述】:
我的服务有以下路线:
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(""));
}
到目前为止,header 仍然为 null,body 也是如此(TEST 变量是直接组件)
【问题讨论】:
-
你能发布你得到的错误吗?堆栈跟踪?
-
自动创建的东西并不是真正的问题;我想模拟这些端点并返回一个响应,因为它不是那些直接在测试中的端点,所以我并不真正关心创建 mybatis 组件
-
在这种情况下,为每个 .to() 设置一个 .id() 然后拦截、替换。并设置硬编码的响应值。请参阅建议进行测试.. camel.apache.org/advicewith.html
-
@Souciance Eqdam Rashti 啊,好的,谢谢。在一个半相关的说明中,你知道我可以使用什么样的表达式通过 MockEndpoint.returnReplyBody 发送一个 hashMap 或一个列表? Simple 对象似乎不适合我想要做的事情
-
它需要使用表达式,但我会改为做一个adviceWith,而是这样写:weaveById("yourid").replace().setBody(yourhashmap);
标签: java mocking apache-camel camel-test