简体   繁体   中英

Mocks ignored and endpoints not skipped in Camel route test

I am trying to mock a Processor in a Camel test for a route containing

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

The test class (simplified):

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
}

When I run the test, no mock is used and I see in the logs that the actual MessageParserProcessor from the registry is used.
Here is a relevant part of the logs:

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*

What is wrong in my setup? I also tried to do:

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

But nothing is printed or logged.

Also I was wondering why I must bind actual beans in the first place in createRegistry() knowing I want to use mocks? (I tried not to but I get errors). It's weird, it's like using a framework such as Mockito to mock objects that we should first create as actual objects...

Why do you have .bean(getMockEndpoint("mock:messageParserProcessor")); ? Shouldn't it be .to("mock:messageParserProcessor")

I usually create mockendpoints like this:

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

Then in my @test method, after context.start() I use the mockendpoint to assert something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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