2016-08-09 57 views
0

我有一個ScheduledExecutorService設置爲每隔幾分鐘更新一些對象。ScheduledExecutorService內的測試代碼

我想測試更新是否發生,當然還有一些邊緣情況。

scheduler.scheduleAtFixedRate(()-> { 
    //Code to update objects 
}, 0, 4, TimeUnit.MINUTES); 

我不知道我將如何去測試,也許嘲笑他,所以我不必依靠的Thread.sleep

回答

0

注意scheduleAtFixedRate方法返回一個Future,所以你不應該使用Thread.sleep ,但是其中的一種方法是Future#get

另一個建議是用一個可配置參數替換硬編碼時間延遲(4分鐘),以便您可以從測試中更改它。