加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用Apache Camel对FTP使用者进行单元测试

发布时间:2020-12-15 05:07:59 所属栏目:Java 来源:网络整理
导读:我有以下路线.在单元测试中,由于我没有可用的FTP服务器,我想使用camel的测试支持并向“ftp:// hostname / input”发送无效消息并验证它是否失败并路由到“ftp: //主机名/错误”. 我浏览了主要讨论使用“mock:”端点的文档,但我不确定如何在这种情况下使用
我有以下路线.在单元测试中,由于我没有可用的FTP服务器,我想使用camel的测试支持并向“ftp:// hostname / input”发送无效消息并验证它是否失败并路由到“ftp: //主机名/错误”.

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

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服务器.后者更像是集成测试,您可以在其中拥有专用的测试环境.

Camel在其测试工具包中非常灵活,如果您想构建一个不使用真实FTP组件的单元测试,那么您可以在测试之前替换它.例如,在您的示例中,您可以将路由的输入端点替换为直接端点,以便更容易向路径发送消息.然后你可以使用拦截器拦截发送到ftp端点,并绕过消息.

部分测试工具包的建议提供了这些功能:http://camel.apache.org/advicewith.html.并且还在Camel in action book的第6章中讨论,例如6.3节,讨论模拟错误.

在你的例子中,你可以做一些类似的事情

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读