骆驼:模拟并从路径中的组件返回值

jba*_*991 1 java mocking apache-camel camel-test

我的服务中有以下路线:

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
}
Run Code Online (Sandbox Code Playgroud)

当我尝试对此运行测试时,它给出了一个错误,说明camel无法自动创建组件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(""));
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,标头仍然是空的,因为正文(TEST变量是直接组件)

Sou*_*hti 5

如果您想要输入硬编码的响应,则可以更轻松地对您的路线进行建议.见这里: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");
    }
});
Run Code Online (Sandbox Code Playgroud)

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

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

下面是一个示例,当您执行adviceWith时,将一个正文添加到简单表达式中.

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

});
Run Code Online (Sandbox Code Playgroud)