使用vm和文件组件在ftp

时间:2015-08-07 12:47:43

标签: unit-testing apache-camel camel-ftp

我有两个非常简单的路由(下面的代码不是原始类,我简化了它就像删除setter,日志等)

第一条路线:

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

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

课堂上的配置部分:

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

问题是接下来的测试,我在骆驼中很安静,并且用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>

请有人帮助我。

1 个答案:

答案 0 :(得分:0)

经过几天的挣扎,我发现了问题所在。我正在向正文发送消息,但此消息将被其他路由使用。我使用的是版本2.10,有绿色单元测试的解决方案是停止路由。对于当前版本(2.10),它会有点复杂,因为我应该更改我的主类只是为了单元测试。但我可以使用controlbus:相反,如果我们将骆驼升级到2.11 +。