在 ftp 上使用 vm 和文件组件测试 Apache Camel 路由

Testing an Apache Camel route with vm and file component over ftp

我有两条非常简单的路线(下面的代码不是原来的classes,我已经简化了它,比如删除了设置器、日志等等)

第一条路线:

public static final String MESSAGE_CONSUMER = "vm:myMessage";

public String myXmlProducer; //and getter method then

myXmlProducer = "file:myFileLocation?autoCreate=true&fileName="+ myFileName;
from(MESSAGE_CONSUMER)
    //some process here
    .to(myXmlProducer);

第二个:

public String fileConsumer = "file:myFileLocation";
public String ftpProducer = "ftp://ftpIp?username=username&password=password&maximumReconnectAttempts=0";

from(fileConsumer)
    .to(ftpProducer );

我正在尝试为这两条路线编写一些测试,这是我测试的一部分 class,

class中的配置部分:

public static final String MOCK_OUT = "mock:out";
public static final String DIRECT_IN = "direct:in";
public static final String MOCK_XML_URI = "mock:xmlFile";
...
@EndpointInject(uri = MOCK_OUT)
MockEndpoint mockOut;

@EndpointInject(uri = MOCK_XML_URI)
MockEndpoint mockXmlUri;
...

@Override
public RouteBuilder[] createRouteBuilders() {
    final MyRoute route = new MyRoute();
    route.setFtpProducer (MOCK_OUT);

    RouteBuilder testHarness = new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from(DIRECT_IN).routeId("testHarness.in")
                .to(MyRoute.MESSAGE_CONSUMER)

            from(route.getMyXmlProducer())
                .convertBodyTo(Byte.class)
                .to(mockXmlUri)
        }
    };
    return new RouteBuilder[] {route, testHarness};
}

@Before
public void getTestData() throws IOException {
    testInputData = IOUtils.toString(this.getClass().getResourceAsStream("/myTarget/inputData.txt"));
}

这个测试方法是绿色的:

@Test
public void testRoutingOk() throws InterruptedException {
    mockOut.setExpectedMessageCount(1);
    mockOut.message(0).body().isEqualTo(testInputData);

    template.sendBody("file:myTestFileLocation", testInputData);
    assertMockEndpointsSatisfied();
}

问题出在下面的测试中,我是camel新手,用google搜索过原因,但自己搞不懂:

@Test
public void testRoutingOk() throws InterruptedException {
    mockXmlUri.setExpectedMessageCount(1);
    mockXmlUri.message(0).body().isEqualTo(testInputData);

    template.sendBody(DIRECT_IN, testInputData);
    assertMockEndpointsSatisfied();
}

我收到资产错误,根本没有消息:

java.lang.AssertionError: mock://xmlFile Received message count. Expected: <1> but was: <0>

请问有没有人可以帮助我。

折腾了几天,终于找到问题所在了。我正在向 body 发送一条消息,但是这条消息将被另一条路线使用。我正在使用 2.10 版,进行绿色单元测试的解决方案是停止路由。对于当前版本 (2.10),它会有点复杂,因为我应该更改我的 main 类 只是为了单元测试。但是我可以使用 controlbus: 相反,如果我们将骆驼升级到 2.11+。