0

我对apache camel完全陌生。我对它有了一些基本的了解。

现在,我正在浏览一些视频和文档,以获得一些想法,为基于apache camel spring DSLspring boot 应用程序实现junit测试用例,但我不清楚,因为有很多方法可以实现或非常高水平。

我很困惑..跟随哪一个以及那些junits中实际发生的事情

有没有人有示例链接视频来解释基于apache camel spring DSLspring boot 应用程序的junit覆盖范围?

我特别在寻找junits。另外,如果您知道有关 apache camel 的好教程,请告诉我。

4

1 回答 1

1

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% 覆盖了我的路由!除了我不这样做,因为它所做的只是在骆驼路线系统中创建了执行模型,并且各种组件和处理器现在都将以正确的顺序运行。

Beans 和Processors 将包含在覆盖率报告中,但如果您在路线中有复杂的逻辑,它不会为您提供覆盖率。

有这个功能,在 2017 年左右交付 - https://issues.apache.org/jira/browse/CAMEL-8657 - 但我没有使用它,并且不确定它如何与你使用的任何覆盖工具一起工作。

于 2020-05-13T23:27:27.817 回答