CPU { public int getUsage() { try { String result = terminal.exec("top -s cpu -n 1"); int cpuUsage = extractCPUUsage(result); return cpuUsage; } catch (IOException e) { Logger.e("Exception catch while reading the CPU usage", e); return 0; } } CPU(App app, Terminal terminal); int getUsage();  }
@Test public void returnsTheCPUUsageIfThereIsNoProblem() throws Exception { givenTheAppPIDIs(ANY_PID); givenTheTerminalReturnsAnExpectedOutput(); int usage = cpu.getUsage(); assertEquals(8, usage); }
@Test public void returnsZeroIfTheTerminalOutputIsNotCorrect() throws Exception { givenTheAppPIDIs(ANY_PID); givenTheTerminalReturnsAnError(); int usage = cpu.getUsage(); assertEquals(0, usage); }
@Test public void returnsZeroIfThePIDIsNotInTheTerminalOutput() throws Exception { givenTheAppPIDIs(-1); givenTheTerminalReturnsAnError(); int usage = cpu.getUsage(); assertEquals(0, usage); }