Java – unit test FTP users using Apache Camel

I have the following route In the unit test, because I don't have an available FTP server, I want to use camel's test support and send an invalid message to "FTP: / / hostname / input", verify whether it fails and route to "FTP: / / hostname / error"

I've browsed through the documentation that focuses on using the "mock:" endpoint, but I'm not sure how to use it in this case

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

Solution

As Ben said, you can set up the FTP server and use real components The FTP server can be embedded or set up internally The latter is more like integration testing, where you can have a dedicated test environment

Camel is very flexible in its test kit. If you want to build a unit test that does not use real FTP components, you can replace it before testing For example, in your example, you can replace the input endpoint of the route with a direct endpoint to make it easier to send messages to the path You can then use interceptors to intercept messages sent to FTP endpoints and bypass messages

Some of the test kit recommendations provide these features: http://camel.apache.org/advicewith.html. It is also discussed in Chapter 6 of camel in action book, for example, section 6.3 discusses simulation errors

In your example, you can do something similar

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

Starting with camel 2.10, we will use suggestions to make interception and simulation easier We also introduced a stub component http://camel.apache.org/stub

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>