In test cases, there are two problems using (real) time:
- It is never determined. Especially if you are looking for high accuracy, test cameras will be successful in 95% of cases. But sometimes they fail; these are the most complex types of debugging. Note that using
Thread.sleep() in a multi-threaded test case is even more complicated. - Tests with dreams take a lot of time, and after a while this will make your complete test suite cumbersome.
If you want your path to be acceptable. But there are other options:
Do not use a real watch. Instead, use a fake (mocked / stubbed) clock, which you can control from your test file:
@Test(expected = IllegalStateException.class) public void testCallAfterTimeout() { MyObject o= new MyObject();
You must enter the hours in your facility. MyObject might look like this:
class MyObject { public MyObject() { this(new Clock()); }
Java 8 provides a mechanism for this; see, for example, LocalDate.now() . But you can also easily realize your own silence. Please note that the Java 8 clock provides additional mechanisms to automatically increase the time you use.
Thirler
source share