You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写Apache Camel测试时遇AdviceWith报错:找不到路由

Apache Camel测试用例AdviceWith报错问题

问题场景

给Apache Camel路由编写测试用例时,运行触发错误,堆栈信息指向代码中的AdviceWith部分,反复检查端点书写无误但问题依旧。

测试代码

@Test
public void testRoute() throws Exception {

    AdviceWith.adviceWith(context, Endpoints.SEDA_SEND_ENDPOINT, a -> {
        a.mockEndpoints(Endpoints.SEDA_SEND_ENDPOINT);
    });
//     Set up the expectations for the mock endpoint

   getMockEndpoint("mock:" + Endpoints.SEDA_SEND_ENDPOINT).expectedMessageCount(1);

    // Send a message to the seda:sendMessage endpoint
    template.sendBody(Endpoints.SEDA_SEND_ENDPOINT, "test message");

    // Verify that the mock endpoint expectations are met
    assertMockEndpointsSatisfied();
 //   
}

报错信息

java.lang.IllegalArgumentException: Cannot advice route as there are no routes
at org.apache.camel.builder.AdviceWith.findRouteDefinition(AdviceWith.java:262)
at org.apache.camel.builder.AdviceWith.adviceWith(AdviceWith.java:74)
at com.sams.pricing.prism.data.processor.CamelRouteTests1.testRoute(CamelRouteTests1.java:26)
at java.base/

端点定义

SEDA_SEND_ENDPOINT = "seda:sendMessage?blockWhenFull=true&concurrentConsumers=100"

问题原因与解决办法

核心问题

AdviceWith的第二个参数要求传入路由ID,而非端点URI。你当前传入的是带参数的完整SEDA端点URI,Camel无法匹配到对应的路由,因此抛出“没有可修改的路由”错误。

解决步骤

  1. 给目标路由添加ID:在业务路由定义时,为路由设置唯一ID,示例如下:
from(Endpoints.SEDA_SEND_ENDPOINT)
    .id("sendMessageRoute") // 设置路由唯一标识
    .to(...) // 后续业务路由逻辑
  1. 修正AdviceWith调用:将第二个参数替换为路由ID,同时可通过通配符简化端点匹配:
AdviceWith.adviceWith(context, "sendMessageRoute", a -> {
    a.mockEndpoints("seda:sendMessage"); // 精确匹配目标端点,或用"seda:*"匹配所有seda类型端点
});
  1. 调整Mock端点验证逻辑:确保Mock端点URI与上面的匹配规则一致,示例:
getMockEndpoint("mock:seda:sendMessage").expectedMessageCount(1);

额外提醒

  • 若使用Camel官方JUnit测试框架,建议在@Before方法中执行AdviceWith操作,确保在Camel上下文启动前完成路由修改
  • 不要用带参数的完整端点URI匹配路由,Camel匹配路由的依据是路由ID或不带参数的基础端点URI,带参数的URI无法匹配到路由定义

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 10:40:51