在骆驼路由测试中忽略模拟和未跳过端点



我试图在骆驼测试中模拟处理器的路由包含

.bean("xxx")
.bean("messageParserProcessor")
.bean("yyy")

测试类(简化):

public class SomeTest extends CamelTestSupport {
@EndpointInject(uri = "mock:messageParserProcessor")
protected MockEndpoint messageParserProcessorMock;
// Other declarations
@Override
public boolean isUseAdviceWith() {
return true;
}
@Before
public void setUpContext() throws Exception {
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
interceptSendToEndpoint("messageParserProcessor")
.skipSendToOriginalEndpoint()
.bean(getMockEndpoint("mock:messageParserProcessor")); // Same as using messageParserProcessorMock
}
});
}
@Test
public void testParser() throws Exception {
context.start();
String expectedBody = "test";
messageParserProcessorMock.expectedBodiesReceived(expectedBody);
ProducerTemplate template = context.createProducerTemplate();
template.sendBody(producerTemplateUri, expectedBody);
messageParserProcessorMock.assertIsSatisfied();
context.stop();
}
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
jndi.bind("messageParserProcessor", new MessageParserProcessor());
// Other bindings
return jndi;
}
// Other methods
}

当我运行测试时,没有使用模拟,我在日志中看到使用了注册表中的实际消息解析器处理器。
以下是日志的相关部分:

Skipping starting CamelContext as isUseAdviceWith is set to true.
AdviceWith route after:
Route[[From[aws-sqs://xxx]] ->
[InterceptSendToEndpoint[messageParserProcessor -> [Bean[mock://messageParserProcessor]]], Bean[ref:messageParserProcessor]]]
*Here I get logs from the actual processor, which I don't expect*

我的设置出了什么问题? 我也尝试做:

interceptSendToEndpoint("messageParserProcessor")
.skipSendToOriginalEndpoint()
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
// Log and print something
}
});

但是没有打印或记录任何内容。

我也想知道为什么我必须在知道我想使用模拟的情况下首先绑定实际的豆子createRegistry()?(我尽量不这样做,但我得到错误)。这很奇怪,就像使用 Mockito 这样的框架来模拟我们应该首先创建为实际对象的对象......

为什么你有.bean(getMockEndpoint("mock:messageParserProcessor"));?不应该是.to("mock:messageParserProcessor")

我通常创建这样的模拟端点:

@Before
public void setUp() throws Exception {
super.setUp();
context.getRouteDefinition("MyRoute").adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveById("MyEndPointId").replace().to("mock:MyMockEndpoint");
}
});

然后在我的@test方法中,context.start()之后,我使用 mockendpoint 断言类似的东西:

getMockEndpoint("mock:MyMockEndpoint").expectedMessageCount(size);

最新更新