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

Camel 4.7.0下如何在CamelTestSupport单元测试中启用useAdviceWith?

Camel 4.7.0 中isUseAdviceWith弃用后的替代方案问题解答

问题背景

我们有多个继承CamelTestSupport的JUnit 5 Camel路由单元测试,原本通过重写isUseAdviceWith方法返回true,避免Camel上下文自动启动,且使用adviceWith时不会触发上下文重启。升级至Camel 4.7.0后,该方法被弃用(关联CAMEL-20785和CAMEL-20843)。尝试在setupResources或@BeforeEach方法中配置testConfigurationBuilder.withUseAdviceWith(true),但发现withUseAdviceWith是protected修饰,无法在测试类中直接调用。

疑问解答

1. 你的理解是否正确?

不完全正确。testConfigurationBuilder的withUseAdviceWith方法被设为protected是有意的封装设计,不允许测试类直接调用——这不是配置useAdviceWith的正确入口。

2. 正确的替代方案

在Camel 4.7.0+中,替代重写isUseAdviceWith的正确方式是重写configureTest方法,通过TestConfigurationBuilder的公开API启用useAdviceWith:

@Override
protected void configureTest(TestConfigurationBuilder builder) {
    builder.useAdviceWith(true);
}

这个方法是CamelTestSupport提供的官方扩展点,专门用于配置测试核心参数。useAdviceWith的配置通过builder.useAdviceWith(boolean)这个公开方法完成,而withUseAdviceWith是builder内部的链式调用实现,设为protected是合理的封装,并非bug。

配置后,Camel上下文会保持未启动状态,使用adviceWith时也不会触发自动重启,和之前isUseAdviceWith返回true的行为完全一致。

内容的提问来源于stack exchange,提问作者Just a student

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 22:28:11