2016-11-17 88 views
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(第一个测试为空,第二个为填充)。我如何去执行骆驼测试的何时何种机制?我已经看过模拟端点骆驼文档,但我无法弄清楚如何应用它,并让它向交易所返回一个值,然后继续该路线(测试的最终结果是检查是否有电子邮件或不具有附接被发送)

编辑:使用两个替换()尝试设置*的方法和与呼叫替换的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=" + "[email protected]" 
           +"&from=" + "[email protected]"); 
       } 
    }); 
    ProducerTemplate prod = context.createProducerTemplate(); 
    prod.send(TEST, exch); 

    assertThat(exch.getIn().getHeader("CamelMyBatisResult"), is(1)); 
    assertThat(exch.getIn().getBody(), is("")); 
} 

迄今为止,标题是仍然空(TEST变量是一个直接组件)

+0

你能后你的错误?堆栈跟踪? –

+0

自动创建东西并不是真正的问题;我想嘲笑那些端点并返回响应,因为它不是那些直接被测试的端点,所以我没有真正关心如何创建mybatis组件 – jbailie1991

+0

在这种情况下,为每个.to()设置一个.id() )然后你拦截,替换。并设置硬编码的响应值。请参阅advicewith进行测试.. http://camel.apache.org/advicewith.html –

回答

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" 
    )); 
    } 

}); 
+0

所以我一直在尝试这一段时间,标头没有被设置跳过路线的其余部分,或者至少每次我检查交换中的断言标题它回来为空,将更新问题与测试 – jbailie1991

+0

当你提到使用setBody插入哈希映射,我会怎么做,从一个表达式?该方法需要一个表达式参数,我无法找到任何资源如何插入在setBody调用 – jbailie1991

+0

@ jbailie1991之前创建的变量,我已经用示例编辑了我的答案。 –