使用Cucumber TestRunner测试Java DSL的Camel路由:如何实现createApplicationContext?
解决Cucumber测试Camel Java DSL路由时的
createApplicationContext实现问题 当你用Java DSL定义Camel路由时,不需要依赖XML配置文件,而是可以通过Spring注解配置类来加载路由,对应的createApplicationContext()方法就可以返回AnnotationConfigApplicationContext而非XML上下文。下面是具体的实现步骤和示例:
1. 定义Java DSL路由的配置类
首先创建一个带@Configuration注解的类,通过@Bean方法注册你的RouteBuilder实例:
import org.apache.camel.builder.RouteBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TestCamelRoutes { @Bean public RouteBuilder myBusinessRoute() { return new RouteBuilder() { @Override public void configure() throws Exception { // 这里写你的Java DSL路由逻辑 from("direct:start") .log("Processing message: ${body}") .to("mock:result"); } }; } }
2. 实现测试类的createApplicationContext()
在你的Cucumber测试类中,继承CamelSpringTestSupport后,返回加载上述配置类的AnnotationConfigApplicationContext即可:
import org.apache.camel.test.spring.CamelSpringTestSupport; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; @RunWith(Cucumber.class) @CucumberOptions(features = "classpath:features", glue = "com.your.package.steps") public class CamelCucumberTestRunner extends CamelSpringTestSupport { @Override protected AbstractApplicationContext createApplicationContext() { // 加载Java配置类,替代XML上下文 return new AnnotationConfigApplicationContext(TestCamelRoutes.class); } // 可选:在测试前初始化Mock端点的预期规则 @Override protected void setUp() throws Exception { super.setUp(); getMockEndpoint("mock:result").expectedMessageCount(1); } }
3. 在Cucumber步骤中使用Camel上下文
如果需要在步骤类中操作Camel上下文(比如发送测试消息、验证Mock端点),可以通过Spring依赖注入获取CamelContext,或者直接引用测试类中的上下文实例(推荐用注入方式更规范):
import org.apache.camel.CamelContext; import org.apache.camel.ProducerTemplate; import org.apache.camel.component.mock.MockEndpoint; import org.springframework.beans.factory.annotation.Autowired; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class RouteTestSteps { @Autowired private CamelContext camelContext; @Autowired private ProducerTemplate producerTemplate; @Given("^a test message is ready$") public void prepareTestMessage() { // 准备测试数据 } @When("^the route processes the message$") public void sendMessageToRoute() throws Exception { producerTemplate.sendBody("direct:start", "Test payload"); } @Then("^the result should be correct$") public void verifyResult() throws Exception { MockEndpoint mockEndpoint = camelContext.getEndpoint("mock:result", MockEndpoint.class); mockEndpoint.assertIsSatisfied(); } }
关键注意点
- 确保你的测试类和配置类都能被Spring扫描到,或者直接在
AnnotationConfigApplicationContext中指定配置类。 - 如果不想单独写配置类,也可以在测试类中定义静态内部配置类,同样能被
AnnotationConfigApplicationContext加载。 CamelSpringTestSupport会自动帮你管理Camel上下文的生命周期,无需手动启动/停止。
内容的提问来源于stack exchange,提问作者Saffie
相关产品推荐
相关产品推荐

