繁体   English   中英

在驼峰路由测试中忽略了嘲笑并且没有跳过端点

[英]Mocks ignored and endpoints not skipped in Camel route test

我正在尝试在骆驼测试中模拟包含以下内容的路由的处理器

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

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

我也想知道为什么我必须在知道要使用模拟的情况下首先在createRegistry()绑定实际的bean? (我尝试不这样做,但出现错误)。 这很奇怪,就像使用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()我使用模拟端点来声明如下内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM