簡體   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