【发布时间】:2021-02-04 23:15:13
【问题描述】:
我正在尝试在一个非常基本的路线上进行单元测试的示例设置,到目前为止我尝试过的所有条目都没有让我将 CamelContext 自动连接。
我的路线有这个,下面是单元测试。
public class SampleRouter1 extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:start") // a real entry will be jms:queue:testqueue
.log("Direct Start Init")
.process(new SampleProcessor1())
.to("mock:output");
}
}
单元测试
@RunWith(CamelSpringBootRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)
public class SampleRouter1Test1 {
@Autowired
protected CamelContext camelContext; // never gets wired in
编辑添加我正在自动装配 UT 中的上下文。
运行单元测试的异常是:qualifying bean of type 'org.apache.camel.CamelContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
还有一个问题是,这是为了测试 SampleRouter 还是您只是对流程类和任何其他支持类进行单元测试?或者您是否使用以下内容对其进行更改,以便将消息传递给虚假的直接队列?
AdviceWith.adviceWith(camelContext, "jms:queue:testqueue", a -> { a.replaceFromWith("direct:start"); });
【问题讨论】:
-
如果在
SampleRouter1Test1类的camelContext字段中添加@Autowired注释会怎样? -
所以我确实有那个``` @Autowired public CamelContext camelContext; ``` 但它总是 null 或者它大喊它无法弄清楚如何自动连接它
-
这能回答你的问题吗? Spring Boot Apache Camel Routes testing
-
嗨罗曼,至少不是完全没有。我尝试使用 CamelAutoConfiguration 但它似乎不想自动连接该对象。并且从我看到的 UT 正在重建路线,这不完全是 id 喜欢做的事情。我喜欢发送到路由并让代码以这种方式流动而不是在测试类中重做
标签: spring-boot apache-camel spring-camel