单元测试FTP消费与Apache骆驼 [英] unit testing ftp consumer with apache camel

查看:164
本文介绍了单元测试FTP消费与Apache骆驼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的路线。在单元测试,因为我没有可用的FTP服务器,我想使用骆驼的测试支持,并发送一个无效的消息FTP://主机名/输入并验证它失败,发送到FTP://主机名/错误。端点:我通过它主要是关于使用模拟会谈的文档了。但不知道如何在这种情况下使用它。鸭preciate有这方面的帮助。

 公共类MyRoute扩展RouteBuilder
{
    @覆盖
    公共无效配置()
    {
        onException的(EdiOrderParsingException.class).handled(真)。要(FTP://主机名/错误);        从(FTP://主机名/输入)
            .bean(新OrderEdiTocXml())
            .convertBodyTo(为String.class)
            .convertBodyTo(Document.class)
            。选择()
            。当(的XPath(/ cXML的/响应/状态/ @文本='OK'))
            。要(FTP://主机名/有效)。否则()
            。要(FTP://主机名/无效);
    }
}


解决方案

随着奔说,你既可以建立一个FTP服务器,并使用真实的成分。 FTP服务器可以嵌入,或者你可以设置FTP服务器的内部。后者更像是一个集成测试,在那里你可以有一个专门的测试环境。

骆驼是在其测试套件非常灵活,如果你想建立一个不使用真正的FTP组件的单元测试,那么你可以替换测试之前。例如,在你的例子可以更换路径的输入端点的直接端点,使其更容易将消息发送给路由。然后你就可以使用拦截器拦截发送到FTP端点,以及绕路消息。

与测试工具包的一部分的建议提供了以下功能: http://camel.apache.org/advicewith.html。也是在行动书,如6.3节骆驼的第6章所讨论的,谈到模拟错误。

在您的例子中,你可以做一个东西像

 公共无效testSendError()抛出异常{
    //首先建议更换输入路线,并赶上发送到FTP服务器
    context.getRouteDefinitions()得到(0).adviceWith(背景下,新AdviceWithRouteBuilder(){
        @覆盖
        公共无效配置()抛出异常{
            replaceFromWith(直接:输入);            //拦截有效信息
            interceptSendToEndpoint(FTP://主机名/有效)
                .skipSendToOriginalEndpoint()
                。要(模拟:有效);            //拦截无效的消息
            interceptSendToEndpoint(FTP://主机名/无效)
                .skipSendToOriginalEndpoint()
                。要(模拟:无效);
        }
    });     //当我们与所有的建议做了,我们必须手动启动
    context.start();    //在嘲笑设置预期
    getMockEndpoint(模拟:无效)。expectedMessageCount(1);
    getMockEndpoint(模拟:有效)。expectedMessageCount(0);    //发送无效消息航线
    template.sendBody(直接:输入,这里的一些无效的内容);    //断言测试还行
    assertMockEndpointsSatisfied();
}

从骆驼2.10起,我们将拦截,并使用与建议嘲笑的时候更容易一些。同时我们引入了一个存根组件。 http://camel.apache.org/stub

I have the below route. In unit test, since I doesn't have the FTP server available, I'd like to use camel's test support and send a invalid message to "ftp://hostname/input" and verify that it failed and routed to "ftp://hostname/error". I gone through the documentation which mainly talks about using the "mock:" endpoint. But not sure how to use it in this scenario. Appreciate any help on this.

public class MyRoute extends RouteBuilder
{
    @Override
    public void configure()
    {
        onException(EdiOrderParsingException.class).handled(true).to("ftp://hostname/error");

        from("ftp://hostname/input")
            .bean(new OrderEdiTocXml())
            .convertBodyTo(String.class)
            .convertBodyTo(Document.class)
            .choice()
            .when(xpath("/cXML/Response/Status/@text='OK'"))
            .to("ftp://hostname/valid").otherwise()
            .to("ftp://hostname/invalid");
    }
}

解决方案

As Ben says you can either setup a FTP server and use the real components. The FTP server can be embedded, or you can setup a FTP server in-house. The latter is more like an integration testing, where you may have a dedicated test environment.

Camel is very flexible in its test kit, and if you want to build an unit test that do not use the real FTP component, then you can replace that before the test. For example in your example you can replace the input endpoint of a route to a direct endpoint to make it easier to send a message to the route. Then you can use an interceptor to intercept the sending to the ftp endpoints, and detour the message.

The advice with part of the test kit offers these capabilities: http://camel.apache.org/advicewith.html. And is also discussed in chapter 6 of the Camel in action book, such as section 6.3, that talks about simulating errors.

In your example you could do something a like

public void testSendError() throws Exception {
    // first advice the route to replace the input, and catch sending to FTP servers
    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            replaceFromWith("direct:input);

            // intercept valid messages
            interceptSendToEndpoint("ftp://hostname/valid")
                .skipSendToOriginalEndpoint()
                .to("mock:valid");

            // intercept invalid messages
            interceptSendToEndpoint("ftp://hostname/invalid")
                .skipSendToOriginalEndpoint()
                .to("mock:invalid");
        }
    });

     // we must manually start when we are done with all the advice with
    context.start();

    // setup expectations on the mocks
    getMockEndpoint("mock:invalid").expectedMessageCount(1);
    getMockEndpoint("mock:valid").expectedMessageCount(0);

    // send the invalid message to the route
    template.sendBody("direct:input", "Some invalid content here");

    // assert that the test was okay
    assertMockEndpointsSatisfied();
}

From Camel 2.10 onwards we will make the intercept and mock a bit easier when using advice with. As well we are introducing a stub component. http://camel.apache.org/stub

这篇关于单元测试FTP消费与Apache骆驼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆