Pangea Pangea - 24 days ago 11x
Java Question

unit testing ftp consumer with apache camel

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

and verify that it failed and routed to
. 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
public void configure()

.bean(new OrderEdiTocXml())


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: 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() {
        public void configure() throws Exception {

            // intercept valid messages

            // intercept invalid messages

     // we must manually start when we are done with all the advice with

    // setup expectations on the mocks

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

    // assert that the test was okay

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.