CPUStat { double usage() { long currentTime = System.nanoTime(); long currentCPUTime = os.getProcessCpuTime(); double usage = usage(currentTime, previousTime, currentCPUTime, previousCPUTime, availableProcessors); previousCPUTime = currentCPUTime; previousTime = currentTime; return usage; } CPUStat(OperatingSystemMXBean os);   }
@Test void usage() { when(os.getProcessCpuTime()).thenReturn(1000L).thenReturn(2000L); double usage = stat.usage(); assertThat(usage).isEqualTo(0); usage = stat.usage(); assertThat(usage).isGreaterThan(0); }
@Test void calculateUsage() { assertThat(stat.usage(200L, 100L, 200L, 100L, 1)) .isEqualTo(1); assertThat(stat.usage(300L, 100L, 200L, 100L, 2)) .isEqualTo(0.25); }