Testing Camel with MockEndpoints

孤街浪徒 提交于 2019-11-30 11:31:15

Adam, there are several ways to do this.

For POJO components, blackbox test them separately from any Camel context/routing to focus on business logic.

If you want to do end-to-end testing of the routes, consider using one of these approaches to validate that each step in the route is satisfied.

  • use NotifyBuilder to build Exchange validation expressions (somewhat complex to get your head around)
  • use AdviceWith to dynamically change the route before its run (add Log/Mock endpoints, etc)

I prefer AdviceWith because its very flexible and leverages the familiar MockEndpoints. For a complete example of this, see this unit test

In short, you will create a unit test to inject MockEndpoints into your route and then validate against them as usual...

context.getRouteDefinition("myRouteId").adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        // mock all endpoints
        mockEndpoints();
    }
});

getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");

template.sendBody("direct:start", "Hello World");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!