JUnit 和 Camel 与 JUnit 和“正常”代码的工作方式不同,据我所知,只有相当基本的方法可以从 JUnit 获取骆驼路线的覆盖范围。骆驼路线是一种处理模型,本质上是需要运行的各个步骤的内存模型,因此您不能使用代码覆盖工具来跟踪执行了哪些部分。
考虑这条路线(在 的子类中RouteBuilder
):
public void configure() throws Exception {
from("jms:queue:zzz_in_document_q")
.routeId("from_jms_to_processor_to_jms")
.transacted()
.log(LoggingLevel.INFO, "step 1/3: ${body}")
.bean(DocBean.class)
.log(LoggingLevel.INFO, "step 2/a3 - now I've got this: ${body}")
.process(new DocProcessor())
.log(LoggingLevel.INFO, "step 3/3 - and finally I've got this: ${body}")
.to("jms:queue:zzz_out_document_q");
}
和一个相关的测试用例,在一个扩展的类中CamelBaseTestSupport
:
@Test
public void testJmsAndDbNoInsert() throws Exception {
long docCountBefore = count("select * from document");
template.sendBody("jms:queue:zzz_in_document_q", new Long(100));
Exchange exchange = consumer.receive("jms:queue:zzz_out_document_q", 5000);
assertNotNull(exchange);
Document d = exchange.getIn().getBody(Document.class);
assertNotNull(d);
long docCountAfter = count("select * from document");
assertEquals(docCountAfter, docCountBefore);
}
当单元测试运行时,应用上下文将运行 configure 方法,所以在我将消息放入队列之前,我已经 100% 覆盖了我的路由!除了我不这样做,因为它所做的只是在骆驼路线系统中创建了执行模型,并且各种组件和处理器现在都将以正确的顺序运行。
Bean
s 和Processor
s 将包含在覆盖率报告中,但如果您在路线中有复杂的逻辑,它不会为您提供覆盖率。
有这个功能,在 2017 年左右交付 - https://issues.apache.org/jira/browse/CAMEL-8657 - 但我没有使用它,并且不确定它如何与你使用的任何覆盖工具一起工作。