3

Im currently working with Camel's mock component and i would like to test it on an existing routes. Basically i want to retain the existing routes defined in the app, but inject a few mocks during test, to verify or at least peek on the current exchange contents.

Based on the docs and from the Apache Camel Cookbook. I've tried to use @MockEndpoints

Here's the route builder

@Component
public class MockedRouteStub extends RouteBuilder {

    private static final Logger LOGGER = LoggerFactory.getLogger(MockedRouteStub.class);

    @Override
    public void configure() throws Exception {
        from("direct:stub")
            .choice()
                .when().simple("${body} contains 'Camel'")
                    .setHeader("verified").constant(true)
                    .to("direct:foo")
                .otherwise()
                    .to("direct:bar")
                .end();

        from("direct:foo")
            .process(e -> LOGGER.info("foo {}", e.getIn().getBody()));

        from("direct:bar")
            .process(e -> LOGGER.info("bar {}", e.getIn().getBody()));

    }

}

Here's my test (currently its a springboot project):

@RunWith(SpringRunner.class)
@SpringBootTest
@MockEndpoints
public class MockedRouteStubTest {

    @Autowired
    private ProducerTemplate producerTemplate;

    @EndpointInject(uri = "mock:direct:foo")
    private MockEndpoint mockCamel;

    @Test
    public void test() throws InterruptedException {
        String body = "Camel";
        mockCamel.expectedMessageCount(1);

        producerTemplate.sendBody("direct:stub", body);

        mockCamel.assertIsSatisfied();
    }

}

message count is 0 and it looks more like @MockEndpoints is not triggered. Also, logs indicate that the log is triggered

route.MockedRouteStub    : foo Camel

An alternative i've tried is to use an advice:

...
        @Autowired
        private CamelContext context;

        @Before
        public void setup() throws Exception {
            context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {

                @Override
                public void configure() throws Exception {
                    mockEndpoints();
                }
            });
        }

The startup logs indicate that advice is in place:

c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [direct://stub] with mock endpoint [mock:direct:stub]

But still my test fails with the message count = 0.

geneqew
  • 2,401
  • 5
  • 33
  • 48
  • I had the same question. I did it in this way. Check my answer here https://stackoverflow.com/questions/42275732/spring-boot-apache-camel-routes-testing/44325673#44325673 – pvpkiran Jun 02 '17 at 09:42
  • You likely need to use `@RunWith(CamelSpringRunner)` to make those Camel annotations enabled. – Claus Ibsen Jun 02 '17 at 09:49
  • @ClausIbsen, tried it with `CamelSpringRunner` instead of `SpringRunner` but i still get the expected message count of 0. Also, log indicate that message got routed to direct:foo – geneqew Jun 02 '17 at 10:16
  • Thanks for sending the link @pvpkiran; ive used one of the answers from the link you've posted. What worked for me is to use _CamelSpringBootRunner_ instead of _CamelSpringRunner_ – geneqew Jun 02 '17 at 11:28
  • Ah yeah didn't spot that you are using Spring Boot. There is a specialized runner for that – Claus Ibsen Jun 02 '17 at 19:04
  • One thing that does not work though is the use of @MockEndpointsAndSkip; Should it still pass given the conditions i posted above? except that it wont proceed (in this case the log message wont be displayed) – geneqew Jun 05 '17 at 01:35

1 Answers1

5

Posting the answer which worked for the setup that i have.

Without any changes to the RouteBuilder, the Test would look something like this:

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints
public class MockedRouteStubTest  {

    @Autowired
    private ProducerTemplate producerTemplate;

    @EndpointInject(uri = "mock:direct:foo")
    private MockEndpoint mockCamel;

    @Test
    public void test() throws InterruptedException {
        String body = "Camel";
        mockCamel.expectedMessageCount(1);

        producerTemplate.sendBody("direct:stub", body);

        mockCamel.assertIsSatisfied();
    }

}
geneqew
  • 2,401
  • 5
  • 33
  • 48