阿帕奇骆驼|弹簧测试|拦截路线无效

时间:2020-06-12 00:29:17

标签: java junit apache-camel spring-camel camel-test

我对apache骆驼不熟悉,因此我仍在努力编写骆驼测试用例。

我在下面定义了路线

from("direct:routeToTest")
        .id(ROUTE_ID)
        .to(LOOK_UP_ROUTE)
        .choice()
           .when(some-condition)        
             .choice()
                .when(condition)
                .to(CREATE_ROUTE)
               .otherwise().process(exchange -> exchange.getIn().setBody(prepareResponse(""))
             .endChoice()
          .otherwise()
            .log("Some Issue")
            .process(exchange -> unknownError(exchange))
        .endChoice();
  }

在测试时,我试图拦截我的路线中定义的 to 并对此设置一些模拟响应。因此,经过一些搜索,我发现使用 adviceWith 是实现此目标的正确方法。

所以我的测试如下。测试的结果是,它仍将使用Look_up_route(direct:lookUpRoute,定义的另一条路由)处理通过的数据,但是期望代码将其跳过并将响应设置为“ MockResponse”

@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@RunWith(CamelSpringBootRunner.class)
@UseAdviceWith
@MockEndpoints
@DisableJmx(false)
public class RouteTest {

  @Autowired
  private ProducerTemplate producerTemplate;

  @Autowired
  private CamelContext context;

 @Test
  public void testResponseToJSON() throws Exception {
    SomeObject someObject = getObject();
    context.getRouteDefinition(ROUTE_ID).adviceWith(context, new AdviceWithRouteBuilder() {
          @Override
          public void configure() throws Exception {
            interceptSendToEndpoint(LOOK_UP_ROUTE)
                .skipSendToOriginalEndpoint()
                .transform("MockOutput");
        }
    );
    context.start();
    Object object = producerTemplate.requestBody(direct:routeToTest, someObject);
  }

}

我想知道如何跳过.to(LOOK_UP_ROUTE)并将其设置为micResponse。

1 个答案:

答案 0 :(得分:0)

您的intercept陈述一见钟情。但是,请查看此答案以找到另一种方法。

您必须

  1. 在您的路线的LOOK_UP_ROUTE步骤中添加一个id
  2. 然后,您可以使用adviceWith删除或操作测试中标记的步骤
  3. 将邮件正文(和标头)设置为测试所需的任何内容