Code example for Stopwatch

Methods: startstoptoString, elapsedTime

0
    public void shouldPrintIterationsTime() throws Exception {
 
        // given 
        Ticker ticker = mock(Ticker.class);
        when(ticker.read()).thenReturn(0L, 2*60*60*1000000000L); // 2 hours
        Stopwatch stopwatch = new Stopwatch(ticker);
 
        // when 
        stopwatch.start();
        // some method is called here 
        stopwatch.stop();
 
        // then 
        assertThat(stopwatch.toString()).isEqualTo("7200 s");
        assertThat(stopwatch.elapsedTime(TimeUnit.MINUTES)).isEqualTo(120);
        assertThat(stopwatch.elapsedTime(TimeUnit.HOURS)).isEqualTo(2);
    } 
 
} 
Experience pair programming with AI  Get Codota for Java