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

Apache Camel 3.2.x升级至3.14.x后SpringBoot测试路由加载异常

问题解决:Camel 3.14.x升级后测试报错“RouteDefinition must be specified”

问题根源

Camel 3.10+版本对@CamelSpringBootTest的行为做了调整:

  • 该注解会自动管理Camel上下文的生命周期,手动调用camelContext.start()会触发冲突
  • 当设置autoStartup=false后,路由定义还未被Spring容器加载,就执行了camelContext.getRouteDefinitions(),导致遍历不到任何路由,进而触发RouteDefinition must be specified错误

修复方案

1. 简化测试类注解与生命周期管理

移除手动启动/停止Camel上下文的代码,改用Camel测试框架的原生机制:

@ExtendWith(SpringExtension.class)
@CamelSpringBootTest
@MockEndpoints("mock:*")
public class CamelRouteTest {

    @Autowired
    private CamelContext camelContext;

    @EndpointInject("mock:result")
    private MockEndpoint mockResult;

    @BeforeEach
    public void setUp() throws Exception {
        // 针对指定ID的路由执行AdviceWith,避免遍历空集合
        AdviceWith.adviceWith(camelContext, "start-route", new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {
                interceptSendToEndpoint("direct:endpoint")
                        .skipSendToOriginalEndpoint()
                        .to("mock:result");
            }
        });
    }

    @Test
    public void testRoute() throws Exception {
        mockResult.expectedMessageCount(1);
        
        camelContext.createProducerTemplate().sendBody("direct:start", "test-data");
        
        mockResult.assertIsSatisfied();
    }
}

2. 给RouteBuilder添加路由ID

在路由定义中显式指定ID,方便AdviceWith精准定位:

@Component
public class MyRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct:start")
                .routeId("start-route") // 添加路由ID
                .to("mock:result");
    }
}

3. 调整配置参数

确保application-test.properties中的Camel配置正确:

# 无需设置autoStartup=false,Camel测试框架会自动控制
camel.springboot.auto-startup=true

关键注意事项

  • 避免同时使用@CamelSpringBootTest和@SpringBootTest,前者已集成Spring Boot测试的核心功能
  • AdviceWith必须在Camel上下文启动前执行,@CamelSpringBootTest会自动保证这个时机
  • 显式指定路由ID是3.10+版本中AdviceWith的推荐用法,避免遍历空路由定义集合

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 11:52:37