### Question:
NwpUtil { static int nearestTimeStep(Array sourceTimes, int targetTime) { int timeStep = 0; int minTimeDelta = Math.abs(targetTime - sourceTimes.getInt(0)); for (int i = 1; i < sourceTimes.getSize(); i++) { final int sourceTime = sourceTimes.getInt(i); final int actTimeDelta = Math.abs(targetTime - sourceTime); if (actTimeDelta < minTimeDelta) { minTimeDelta = actTimeDelta; timeStep = i; } } return timeStep; } private  NwpUtil();   }### Answer:
@Test public void testNearestTimeStep_oneTimeValue() { when(array.getSize()).thenReturn(1L); when(array.getInt(0)).thenReturn(15); final int nearestTimeStep = NwpUtil.nearestTimeStep(array, 18); assertEquals(0, nearestTimeStep); }
@Test public void testNearestTimeStep_twoTimeValues() { when(array.getSize()).thenReturn(2L); when(array.getInt(0)).thenReturn(15); when(array.getInt(1)).thenReturn(19); final int nearestTimeStep = NwpUtil.nearestTimeStep(array, 18); assertEquals(1, nearestTimeStep); }
@Test public void testNearestTimeStep_twoTimeValues_invertedOrder() { when(array.getSize()).thenReturn(2L); when(array.getInt(0)).thenReturn(19); when(array.getInt(1)).thenReturn(15); final int nearestTimeStep = NwpUtil.nearestTimeStep(array, 18); assertEquals(0, nearestTimeStep); }
@Test public void testNearestTimeStep_threeTimeValues() { when(array.getSize()).thenReturn(3L); when(array.getInt(0)).thenReturn(21); when(array.getInt(1)).thenReturn(17); when(array.getInt(2)).thenReturn(13); final int nearestTimeStep = NwpUtil.nearestTimeStep(array, 18); assertEquals(1, nearestTimeStep); }