TimeExpirable implements Expirable { @Override public boolean isExpired() { return (now.now() - creationTime) >= expirationIntervalInMs; } TimeExpirable();  TimeExpirable(int expirationIntervalInMinutes);   TimeExpirable(int expirationIntervalInMinutes, Now now); @Override boolean isExpired();  }
@Test public void testHasExpired() throws Exception { TimeExpirable expirable = new TimeExpirable(0); assertThat(expirable.isExpired()).isTrue(); expirable = new TimeExpirable(1); assertThat(expirable.isExpired()).isFalse(); }
@Test public void testHasExpired_SimulatingExpiration() throws Exception { TimeExpirable.Now now = mock(TimeExpirable.Now.class); long time = System.currentTimeMillis(); when(now.now()).thenReturn(time); TimeExpirable expirable = new TimeExpirable(1, now); when(now.now()).thenReturn(time + MIN_IN_MS); assertThat(expirable.isExpired()).describedAs("Exactly at expiration interval").isTrue(); }
@Test public void testHasExpired_SimulatingNonExpiration() throws Exception { TimeExpirable.Now now = mock(TimeExpirable.Now.class); long time = System.currentTimeMillis(); when(now.now()).thenReturn(time); TimeExpirable expirable = new TimeExpirable(1, now); when(now.now()).thenReturn(time + 100); assertThat(expirable.isExpired()).isFalse(); }