### Question:
CPUMonitor { public double getCPULoadAverage() { if (osMBean == null) { return -1; } synchronized (monitor) { if (cpuData.isCacheValid()) { return cpuData.getValue(); } double systemLoadAverage = osMBean.getSystemLoadAverage(); int availableProcessors = osMBean.getAvailableProcessors(); if (systemLoadAverage < 0) { cpuData.setValue(systemLoadAverage); } else { if (availableProcessors == 0) { cpuData.setValue(-2); } else { cpuData.setValue(systemLoadAverage / availableProcessors); } } StringBuilder sb = new StringBuilder(); sb.append("CPU load average:").append(cpuData.getValue()); sb.append(", system load average:").append(systemLoadAverage); sb.append(", available processors:").append(availableProcessors); cpuData.setAdditionalData(KEY_DESCRIPTION, sb.toString()); double result = cpuData.getValue(); LOG.trace("Checked cpu usage, value now:{}", result); return result; } } CPUMonitor(OperatingSystemMXBean osMBean, long cacheTimeInMilliseconds); double getCPULoadAverage(); String getDescription(); void setCacheTimeInMillis(long cacheTimeInMilliseconds);  }### Answer:
@Test public void one_processor_system_load_average_0_dot_5_results_cpu_load_0_5() { when(bean.getAvailableProcessors()).thenReturn(1); when(bean.getSystemLoadAverage()).thenReturn(0.5); assertEquals(0.5, monitorToTest.getCPULoadAverage(),0.01); }
@Test public void two_processors_system_load_average_2_percent_results_cpu_load_1() { when(bean.getAvailableProcessors()).thenReturn(2); when(bean.getSystemLoadAverage()).thenReturn(2.0); assertEquals(1.0, monitorToTest.getCPULoadAverage(),0.01); }
@Test public void two_processors_system_load_average_1_percent_results_cpu_load_0_5() { when(bean.getAvailableProcessors()).thenReturn(2); when(bean.getSystemLoadAverage()).thenReturn(1.0); assertEquals(0.5, monitorToTest.getCPULoadAverage(),0.01); }
@Test public void four_processors_system_load_average_1_percent_results_cpu_load_0_25() { when(bean.getAvailableProcessors()).thenReturn(4); when(bean.getSystemLoadAverage()).thenReturn(1.0); assertEquals(0.25, monitorToTest.getCPULoadAverage(),0.01); }