天天看點

java測試ftp,java – 使用Apache Camel對FTP使用者進行單元測試

正如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