java - 如何在 CDI 环境中模拟现有端点?
问题描述
我不能在模拟端点上断言。我在带有 Java DSL 路由的 WildFly 容器中使用 JUnit 和 CDI 注入。
- 我创建了一个扩展 CamelTestSupport 的测试类
- 我注入了一个 CamelContext 字段用于停止不同的组件和使用建议
- 我设置了 useadvicewith 所以上下文不会自行启动
- 测试类继承了 ModelCamelContext 类型的上下文,这似乎与我注入的 CamelContext 字段无关
- 所有路由都是在注入的 CamelContext 字段中创建的,而不是在继承的 ModelCamelContext 中
- 但是 CamelTestSupport 的整个模拟机制仅使用内部 ModelCamel 上下文
...所以无论我嘲笑什么,都不是我的建议:) 并且所有断言都理所当然地失败了。
@RunWith(Arquillian.class)
public class MyTest extends CamelTestSupport {
@Override
public boolean isUseAdviceWith() {
return true;
}
@Inject
private CamelContext camelContext;
@Test
public void someTest() {
camelContext.getRouteDefinition("routeid").adviceWith(camelContext,
new AdviceWithRouteBuilder() {
@Override
public void configure () throws Exception {
mockEndpoints();
}
});
Collection list = context.getEndpoints(); // the ModelCamelContext is always empty, looks bad
Collection list2 = camelContext.getEndpoints(); // has both route endpoints AND mocks, looks good
getMockEndpoint("mock:someid").expectedMessageCount(1); // will always CREATE a new endpoint in the empty modelcamelcontext
getMockEndpoint("mock:someid", false).expectedMessageCount(1); // will always crash, that endpoint ID is in the other context...
我会放弃注入的 CamelContext 但所有路由都只在那里创建,我不知道为什么。CamelTestSupport 的 ModelCamelContext 上下文总是空的……有些东西我初始化不同/错误,但我不能说什么。有人成功地使用了模拟的 Camel CDI 测试吗?