Timer { public void time(Runnable target) { if (stopwatch.isRunning()) { throw new IllegalStateException("This timer is already timing something"); } try { started(); target.run(); } finally { stop(); } } @Inject  Timer(EventBus eventBus, @Assisted Object target); @VisibleForTesting  Timer(EventBus eventBus, Object target, Stopwatch stopwatch); synchronized void started(); synchronized void stop(); synchronized void reset(); void time(Runnable target); long getElapsedTime();  }
@Test public void testTime() { Timer timer = new MockTimer(eventBus, this); AtomicBoolean ran = new AtomicBoolean(false); timer.time(() -> ran.set(true)); assertEquals(WRONG_TIME_MSG, 1_000_000, timer.getElapsedTime()); assertTrue("Did not run", ran.get()); }