1

我的服务中有以下路线:

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 变量是直接组件)

4

1 回答 1

5

如果你想输入硬编码的响应,在你的路线上做一个 adviceWith 会更容易。见这里:http ://camel.apache.org/advicewith.html

基本上,为每个端点添加一个 id 或.to(). 然后你的测试执行一个adviceWith,然后用一些硬编码的响应替换那个.to()。它可以是地图、字符串或您想要的任何其他内容,它将被替换。例如:

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        // weave the node in the route which has id = bar
        // and replace it with the following route path
        weaveById("bar").replace().multicast().to("mock:a").to("mock:b");
    }
});

请注意,它在文档中说您需要覆盖 isAdviceWith 方法并手动启动和停止 camelContext。

如果您遇到问题,请告诉我们。开始可能有点棘手,但是一旦掌握了窍门,模拟响应实际上非常强大。

这是一个示例,它在您执行 adviceWith.. 时将主体添加到简单表达式中。

context.getRouteDefinition("yourRouteId").adviceWith(context, new AdviceWithRouteBuilder() {
  @Override
  public void configure() throws Exception {
    weaveById("yourEndpointId").replace().setBody(new ConstantExpression("whateveryouwant"
     ));
  }

});
于 2016-11-17T14:45:36.913 回答