@Test public void noAlertingStates() throws Exception { alertingScheduler.run(); verifyZeroInteractions(thresholdChecker); verifyZeroInteractions(executorService); }
@Test public void thresholdCheckerThrowsException() throws Exception { AlertingDefinition definitionOne = mock(AlertingDefinition.class); alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); doThrow(RuntimeException.class).when(thresholdChecker).checkThreshold(any(AlertingState.class)); alertingScheduler.run(); verify(thresholdChecker).checkThreshold(any(AlertingState.class)); verifyNoMoreInteractions(thresholdChecker); verifyZeroInteractions(executorService); } }
@Test public void checkExistingAlertingStates() throws Exception { AlertingDefinition definitionOne = mock(AlertingDefinition.class); AlertingDefinition definitionTwo = mock(AlertingDefinition.class); when(definitionOne.getTimeRange(any(TimeUnit.class))).thenReturn(1L); when(definitionTwo.getTimeRange(any(TimeUnit.class))).thenReturn(3600000L); alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionOne)); alertingScheduler.onApplicationEvent(new AlertingDefinitionCreatedEvent(this, definitionTwo)); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { ((AlertingState) invocation.getArguments()[0]).setLastCheckTime(System.currentTimeMillis()); return null; } }).when(thresholdChecker).checkThreshold(any(AlertingState.class)); alertingScheduler.run(); // both are checked Thread.sleep(10); alertingScheduler.run(); // only first is checked ArgumentCaptor<AlertingState> stateCaptor = ArgumentCaptor.forClass(AlertingState.class); verify(thresholdChecker, times(3)).checkThreshold(stateCaptor.capture()); verifyNoMoreInteractions(thresholdChecker); verifyZeroInteractions(executorService); assertThat(stateCaptor.getAllValues().get(0).getAlertingDefinition(), equalTo(definitionOne)); assertThat(stateCaptor.getAllValues().get(1).getAlertingDefinition(), equalTo(definitionTwo)); assertThat(stateCaptor.getAllValues().get(2).getAlertingDefinition(), equalTo(definitionOne)); }