【问题标题】:Camel processor unit/integration testingCamel 处理器单元/集成测试
【发布时间】:2016-03-18 18:18:35
【问题描述】:

我可能完全错过了一些东西,但我无法按我的意愿测试我的路线。

我有以下 bean:

@Component("fileProcessor")
public class FileProcessor {
   public boolean valid(@Header("customObject) CustomObject customObject,Exchange exchange) throws IOException{
       return false;
}

我有一条这样调用我的 bean 的路由:

from("direct:validationFile").routeId("validationFile").validate().method("fileProcessor","valid")
        // Other stuff
        .end();

这是我的单元测试,基于我找到的一个示例:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class})
@ContextConfiguration(locations = { "classpath:/tu-dao-beans.xml" })
public class FileProcessorTest extends CamelTestSupport {

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

    @Produce(uri = "direct:start")
    protected ProducerTemplate template;

    @Override
    public boolean isDumpRouteCoverage() {
        return true;
    }

    @Test
    public void testSendMatchingMessage() throws Exception {
        String expectedBody = "<matched/>";
        resultEndpoint.expectedBodiesReceived(expectedBody);
        template.sendBodyAndHeader(expectedBody, "foo", "bar");
        resultEndpoint.assertIsSatisfied();
    }

    @Test
    public void testSendNotMatchingMessage() throws Exception {
        resultEndpoint.expectedMessageCount(0);
        template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue");
        resultEndpoint.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
//                from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
                from("direct:start").routeId("validationFile").validate().method("fileProcessor","valid").to("mock:result");
            }
        };
    }
}   

测试失败,因为未找到 fileProcessor,但我很确定我的 spring context 已正确加载,我正在为我的 dbunit 测试使用相同的 beans.xml文件,并且找到了我的 DAO 组件很好......我错过了什么?

编辑: 感谢 Jérémis B 的回答,我轻松解决了我的问题。万一有人像我一样绊倒,我添加的代码是:

@Autowired
private FileProcessor fileProcessor;

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry registry = super.createRegistry();
    registry.bind("fileProcessor", fileProcessor);
    return registry;
}

【问题讨论】:

标签: java spring unit-testing apache-camel spring-test


【解决方案1】:

您可以查看 official documentation 以了解 Spring 的“如何”测试。

在您的示例中,您创建了一个 Spring 上下文,但使用了 CamelTestSupport :此类创建一个不知道 Spring 上下文的 CamelContext。此上下文看不到 bean“fileProcessor”。

有很多方法可以进行这种测试。使用您已经拥有的代码,最简单的方法可能是:

  • 使用@Autowire 在您的测试类中注入fileProcessor
  • 覆盖 createRegistry 并将 fileProcessor 添加到注册表

您也可以覆盖CamelSpringTestSupport 并实现createApplicationContext。另一种方法是将路由定义保存在 Spring Bean 中(通过 xml 或 RouteBuilder),并在您的测试中注入 MockEndpoints 或 ProducerTemplate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-28
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    相关资源
    最近更新 更多