忽略模拟并在Camel路由测试中不跳过端点

时间:2016-12-10 23:58:24

标签: java apache-camel

我正在尝试在Camel测试中为包含

的路由模拟处理器
.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
}

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

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
        }
    });

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

此外,我想知道为什么我必须首先绑定实际的bean createRegistry()知道我想使用模拟? (我试着不要,但我得到错误)。这很奇怪,就像使用像Mockito这样的框架来模拟我们应该首先创建的对象......

1 个答案:

答案 0 :(得分:1)

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

我通常会创建这样的mockendpoints:

@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);