【问题标题】:Apache Camel | Spring Testing | Intercept Route is not working阿帕奇骆驼 |弹簧测试 |拦截路线不起作用
【发布时间】:2020-10-01 18:18:30
【问题描述】:

我是 apache camel 的新手,所以我仍在努力编写骆驼测试用例。

我定义了以下路线

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) 并将 mockResponse 设置为它。

【问题讨论】:

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


    【解决方案1】:

    您的intercept 声明乍一看还不错。但是,请查看此答案以获取替代方法。

    你必须

    1. 在路径中的 LOOK_UP_ROUTE 步骤中添加 id
    2. 然后您可以使用adviceWith 删除或操作测试中的标记步骤
    3. 将消息正文(和标头)设置为适合您的测试的任何内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      相关资源
      最近更新 更多