2016-02-29 53 views
1

我有一個scala類,它使用java nio WatchService來檢測特定目錄中新文件夾的創建。WatchService不能在集成測試中工作

WatchService在應用程序運行時工作正常,我手動將文件夾複製到目標文件夾。

我創建使用scalatest一個單元測試,初始化我的課,並複製一個測試文件夾到使用Apache下議院

FileUtils.copyDirectory(testFolder, new File(targetFolder, testFolder.getName), false) 

手錶服務不會檢測在目標文件夾中創建的任何新條目的目標文件夾30秒。我的代碼是在類似的最終塊內

eventually(timeout(Span(30, Seconds)), interval(Span(1, Seconds))) { 
    // CHECK IF THE SERVICE DETECTED THE NEW ENTRY 
} 

任何想法爲什麼這不起作用的單元測試?

+0

不知道這是否是相關的,但在的javadoc'FileUtils.copyDirectory'它說'注意:此方法試圖保留這些文件'使用File.setLastModified(long)'修改日期/時間。也許在複製後嘗試更改文件夾的最後修改日期作爲測試設置的一部分? –

回答

1

剛發現問題出在我用scalatest的方式。我試圖用一個夾具打開/關閉我的服務功能界限:

describe("The WatchService") { 
    withWatchService { watchService => 
    it("should test feature 1") { /* test code here */ } 
    it("should test feature 2") { /* test code here */ } 
    } 
} 

上面的代碼不工作:完成該功能之前手錶服務被關閉。爲了使它工作,我已經創建了嵌套在夾具一個獨特的功能:

describe("The WatchService") { 
    it("should test features") { 
    withWatchService { watchService => 
     /* test code here */ 
    } 
    }  
}