使用 Apache Camel 对 FTP 使用者进行单元测试 [英] Unit testing FTP consumer with Apache Camel

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

问题描述

我有以下路线.在单元测试中,由于我没有可用的 FTP 服务器,我想使用骆驼的测试支持并向 "ftp://hostname/input" 发送无效消息并验证它失败并路由到 "ftp://hostname/error".

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 an invalid message to "ftp://hostname/input" and verify that it failed and routed to "ftp://hostname/error".

我浏览了主要讨论使用mock:"端点的文档,但我不确定如何在这种情况下使用它.

I gone through the documentation which mainly talks about using the "mock:" endpoint but I am not sure how to use it in this scenario.

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");
    }
}

推荐答案

正如 Ben 所说,您可以设置 FTP 服务器并使用真正的组件.可以嵌入 FTP 服务器,也可以在内部设置 FTP 服务器.后者更像是集成测试,您可能拥有专用的测试环境.

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 的测试套件非常灵活,如果您想构建一个不使用真正的 FTP 组件的单元测试,那么您可以在测试之前替换它.例如,在您的示例中,您可以将路由的输入端点替换为直接端点,以便更轻松地向路由发送消息.然后你可以使用拦截器拦截发送到ftp端点的消息,并绕过消息.

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.

部分测试套件的建议提供以下功能:http://camel.apache.org/advicewith.html.并且也在 Camel in action book 的第 6 章(例如第 6.3 节)中讨论了模拟错误.

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();
}

从 Camel 2.10 开始,我们将在使用通知时使拦截和模拟更容易一些.我们还引入了一个存根组件.http://camel.apache.org/stub

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

这篇关于使用 Apache Camel 对 FTP 使用者进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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