タイマー クラスを作成し、junit4 でテストしています。タイマーは別のクラスに時間を通知し、メソッドをアクティブにします。テスト中に、オブザーバーに送信される値を確認できません。これは、コンパイラーがタイマーに使用される常にループでスタックするためです。タイマーのテストと開始コードを次に示します。
public void testSimpleTimerAsThread() throws InterruptedException
{
SimpleTimer st = new SimpleTimer(1000);
st.start();
Thread.sleep(250); // So we are 1/4th a second different
for (int x=0;x<5;x++)
{
assertEquals(x, st.getRound()); // assumes round starts at
Thread.sleep(1000); // wait for the next time change
}
}
public void start()
{
while (flag)
{
timeChanged();
try
{
// Sleep for var seconds.
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
}
助けてくれてありがとう。