介绍 Camel 如何使用单元测试
camel 测试方式 CamelTestSupport
引入 maven 依赖
1 2 3 4 5 <dependency > <groupId > org.apache.camel</groupId > <artifactId > camel-test</artifactId > <scope > test</scope > </dependency >
编写一个 RouteBuilder
1 2 3 4 5 6 7 8 9 10 11 public class SimpleTransformRoute extends RouteBuilder { @Override public void configure () throws Exception { from("direct:in" ) .transform(simple("Modified: ${body}" )) .to("mock:out" ); } }
编写单元测试
创建一个继承自 org.apache.camel.test.junit4.CamelTestSupport 的类
重写 createRouteBuilder 方法,返回自定义的 RouteBuilder
编写单元测试方法
测试消息通过 CamelTestSupport 的 template(ProducerTemplate 实例)进行发送
MockEndpoint 用于对断言结果进行验证
assertMockEndpointsSatisfied 方法用于触发验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class SimpleTransformRouteTest extends CamelTestSupport { @Override protected RouteBuilder createRouteBuilder () throws Exception { return new SimpleTransformRoute (); } @Test public void testPayloadIsTransformed () throws InterruptedException { MockEndpoint mockOut = getMockEndpoint("mock:out" ); mockOut.setExpectedMessageCount(1 ); mockOut.message(0 ).body().isEqualTo("Modified: Cheese" ); template.sendBody("direct:in" , "Cheese" ); assertMockEndpointsSatisfied(); } }
使用 DI 进行测试
通过注解的方法指定路由生产端点和到达的端点
调用方法可以去掉缺省的端点 url
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class SimpleTransformDIRouteTest extends CamelTestSupport { @Produce(uri = "direct:in") private ProducerTemplate producerTemplate; @EndpointInject(uri = "mock:out") private MockEndpoint mockOut; @Override protected RouteBuilder createRouteBuilder () throws Exception { return new SimpleTransformRoute (); } @Test public void testPayloadIsTransformed () throws InterruptedException { mockOut.setExpectedMessageCount(1 ); mockOut.message(0 ).body().isEqualTo("Modified: Cheese" ); template.sendBody("Cheese" ); assertMockEndpointsSatisfied(); } }
CamelContext 使用 CamelContext 初始化创建路由进行测试,这是使用 Camel 最原始的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class FirstPrinciplesRouteBuilderTest { private CamelContext camelContext; @Before public void setUpContext () throws Exception { this .camelContext = new DefaultCamelContext (); camelContext.addRoutes(new SimpleTransformRoute ()); camelContext.start(); } @After public void cleanUpContext () throws Exception { camelContext.stop(); } @Test public void testPayloadIsTransformed () throws InterruptedException { MockEndpoint out = camelContext.getEndpoint("mock:out" , MockEndpoint.class); out.setExpectedMessageCount(1 ); out.message(0 ).body().isEqualTo("Modified: Cheese" ); ProducerTemplate producerTemplate = camelContext.createProducerTemplate(); producerTemplate.sendBody("direct:in" , "Cheese" ); out.assertIsSatisfied(); } }
自定义测试上下文配置 下面测试代码做了以下自定义配置
添加了 seta 组件
自定义了 RouteBuilder,而不是采用单独的类引用方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public class CustomCamelContextConfigTest extends CamelTestSupport { @Override public CamelContext createCamelContext () { CamelContext context = new DefaultCamelContext (); context.addComponent("activemq" , new SedaComponent ()); return context; } @Override public RouteBuilder createRouteBuilder () { return new RouteBuilder () { @Override public void configure () { from("direct:in" ) .to("activemq:orders" ); from("activemq:orders" ) .to("mock:out" ); } }; } @Test public void testMessagesFlowOverQueue () throws InterruptedException { MockEndpoint out = getMockEndpoint("mock:out" ); out.setExpectedMessageCount(1 ); out.expectedBodiesReceived("hello" ); template.sendBody("direct:in" , "hello" ); assertMockEndpointsSatisfied(); } }
使用 MockEndpoint 验证路由逻辑