おそらく、このクラスをテストするのは意味がありませんが、たとえば、プロジェクト内でテスト カバレッジが 0% のクラスを持っているのがあなただけである場合など、主張する場合は ;-) ここで解決策を示します。
public void testClock() throws InterupteExcdeption {
Clock testClock = new Clock();
long time1 = testClock.getCurrentTime();
Thread.sleep(1000);
long time2 = testClock.getCurrentTime();
assertTrue(time2 > time1);
}
public void testClock2() throws InterupteExcdeption {
Clock testClock = new Clock();
long time1 = testClock.getCurrentTime();
Thread.sleep(1000);
long time2 = testClock.getCurrentTime();
assertTrue(time2 - time1 > 800);
long time3 = testClock.getCurrentTime();
long time4 = testClock.getCurrentTime();
// expecting a value of 0, but make the test defensive, use 100ms
assertTrue(time4 - time3 < 100);
}
さらにヒント: クロック クラスはIClock
インターフェイスを実装する必要があります。そうしないと、システム クロックをテスト ケースのモックに置き換えることが難しくなります。
IClock の考え方は、たとえば 10 倍高速に実行される MockClock を設定できること、または getCurrentTime() によって返される時間を設定できる場所を設定できることです。