[英]Camel testing with Thread.sleep()
在《駱駝在行動》一書中,我找到了這個測試示例:
@Test
public void testMoveFile() throws Exception {
// create a new file in the inbox folder with the name hello.txt and containing Hello World as body
template.sendBodyAndHeader("file://target/inbox", "Hello World", Exchange.FILE_NAME, "hello.txt");
// wait a while to let the file be moved
Thread.sleep(2000);
// test the file was moved
File target = new File("target/outbox/hello.txt");
assertTrue("File should have been moved", target.exists());
// test that its content is correct as well
String content = context.getTypeConverter().convertTo(String.class, target);
assertEquals("Hello World", content);
}
顯然,這是一個集成測試-我們測試多個單元,但是:
我同意大鋁。 如果可以避免,則不應該使用Thread.sleep;如果這樣做(在測試前端行為時這很常見),您肯定要循環一段指定的時間,如果未在X中完成則失敗毫秒。
在我看來,使用線程睡眠往往會創建脆弱或緩慢的測試套件。 您無法保證環境是否會在特定時間內運行測試。
另外,我建議您不要使用單元測試來檢查文件是否已移動。 這意味着您的測試依賴於處於特定狀態的文件系統,並且從技術上講並不是在測試最小的東西。
單元測試必須根據定義:
使用Cucumbers來測試此行為或進行其他更高級別的測試會更好。 要么模擬出與文件系統的交互。
那么它的雞肉和雞蛋。 就像在《駱駝在行動》一書中一樣,稍后我們將向您介紹NotifyBuilder,它使您無需線程休眠即可進行測試。 參見第6章,第6.4.2節。
Camel網站http://camel.apache.org/notifybuilder.html上還有一些有關NotifyBuilder的細節。
我將建立一個循環,不進行睡眠就進行第一次檢查(避免不必要的睡眠); 如果文件尚不存在,請睡眠並重復最多N次,如果未在足夠的時間內創建文件,則失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.