PolledAssertion { public int check() { return check(getTimeoutSeconds()); }  int await(); int await(float seconds); int await(TimeUnit unit, long length); int check(); int check(float seconds); int check(TimeUnit timeUnit, long count);  }
@Test public void testCheckedConditionPasses() throws Exception { final boolean[] condition = {true}; final AtomicLong count = new AtomicLong(); new PolledAssertion() { protected void validate() { count.incrementAndGet(); assertTrue(condition[0]); } }.check(0.3f); assertTrue("Expect count > 2", count.get() > 2); }
@Test public void testCheckedConditionFails() throws Exception { final boolean[] condition = {false}; final AtomicLong count = new AtomicLong(); try { new PolledAssertion() { protected void validate() { count.incrementAndGet(); assertTrue(condition[0]); } }.check(0.3f); } catch (AssertionError ae) { } assertTrue("Expect count == 1", count.get() == 1); }
@Test public void testICanFailImmediatelyWhileAwaiting() throws Exception { long timeStart = System.currentTimeMillis(); try { new PolledAssertion() { protected void validate() { throw new FailImmediatelyException("Fail Immediately"); } }.check(10); } catch (AssertionError ae) { } assertTrue("Expect time < 1s", ((System.currentTimeMillis() - timeStart) / 1000) < 1); }
@Test public void testICanFailImmediatelyIfFailImmediatelyIsCause() throws Exception { long timeStart = System.currentTimeMillis(); try { new PolledAssertion() { protected void validate() throws InvocationTargetException { FailImmediatelyException f = new FailImmediatelyException("Fail Immediately"); throw new InvocationTargetException(f); } }.check(10); } catch (AssertionError ae) { } assertTrue("Expect time < 1s", ((System.currentTimeMillis() - timeStart) / 1000) < 1); }