### Question:
ApplicationLifecycleManager { public void ensureStopped() throws InterruptedException { for (int i = 0; i < 30; i++) { ApplicationStatus currentStatus = detailSupplier.get().applicationStatus(); switch (currentStatus) { case RUNNING: stop(); break; case STOPPING: break; case READY: return; default: throw new IllegalStateException("application in unexpected state: " + currentStatus); } Thread.sleep(statusPollingInterval); } throw new IllegalStateException("timed out waiting for application to stop"); } ApplicationLifecycleManager(String applicationName);  ApplicationLifecycleManager(KinesisAnalyticsClient client,
                                       String applicationName,
                                       long statusPollingIntervalMs); void ensureRunning(); void ensureStopped();  }### Answer:
@Test public void ensureStopped_GivenReadyApplication_ShouldRequestStatusExactlyOnce() throws InterruptedException { Mockito.when(client.describeApplication(describeApplicationRequest)) .thenReturn(withStatus(ApplicationStatus.READY)); manager.ensureStopped(); Mockito.verify(client).describeApplication(describeApplicationRequest); }
@Test public void ensureStopped_GivenDeletingApplication_ShouldThrow() throws InterruptedException { Mockito.when(client.describeApplication(describeApplicationRequest)) .thenReturn(withStatus(ApplicationStatus.DELETING)); try { manager.ensureStopped(); Assert.fail("expected exception stopping an application that is being deleted"); } catch (IllegalStateException expectedException) { } Mockito.verify(client).describeApplication(describeApplicationRequest); }
@Test public void ensureStopped_GivenTimeoutWaitingForStopped_ShouldThrow() throws InterruptedException { Mockito.when(client.describeApplication(describeApplicationRequest)) .thenReturn(withStatus(ApplicationStatus.STOPPING)); try { manager.ensureStopped(); Assert.fail("expected timeout waiting for application to finish starting"); } catch (IllegalStateException expectedException) { } Mockito.verify(client, Mockito.times(30)) .describeApplication(describeApplicationRequest); }