### Question:
ThreadMonitor { public void watchThread(final ExecutorService service, final Runnable runnable) { Reject.ifNull(service, runnable); workPool.submit(new WatchDog(new StartThread() { public Future<?> start() { return service.submit(runnable); } @Override public String toString() { return "Executable: " + runnable.toString(); } })); } @Inject  ThreadMonitor(ExecutorService workPool,
                         ShutdownManager shutdownManager,
                         @Named(SharedGuiceModule.DEBUG_THREAD_MANAGER) Debug debug); void watchThread(final ExecutorService service, final Runnable runnable); void watchScheduledThread(final ScheduledExecutorService scheduledService,
                                     final Runnable runnable,
                                     final long delay,
                                     final long duration,
                                     final TimeUnit timeUnit);  }### Answer:
@Test(expectedExceptions = NullPointerException.class) public void shouldNotExecuteNullRunnable() { monitor.watchThread(mockWorkPool, null); }
@Test(expectedExceptions = NullPointerException.class) public void shouldNotAllowNullExecutorService() { monitor.watchThread(null, mock(Runnable.class)); }
@Test public void shouldSubmitRunnableToProvidedExecutor() { Runnable mockRunnable = mock(Runnable.class); ExecutorService mockService = mockNoOpExecutor(); monitor.watchThread(mockService, mockRunnable); verify(mockService).submit(eq(mockRunnable)); }
@Test public void shouldRespondToShutdownSignalBeforeExecute() { Runnable mockRunnable = mock(Runnable.class); doAnswer(new Answer() { public Object answer(InvocationOnMock invocationOnMock) throws Throwable { ShutdownListener listener = (ShutdownListener) invocationOnMock.getArguments()[0]; listener.shutdown(); return null; } }).when(mockShutdownWrapper).addShutdownListener(any(ShutdownListener.class)); monitor.watchThread(immediateExecutor(), mockRunnable); verify(mockRunnable, times(0)).run(); }
@Test public void shouldRespondToShutdownDuringExecution() throws ExecutionException, InterruptedException { Runnable mockRunnable = mock(Runnable.class); Future mockFuture = mock(Future.class); final ArgumentCaptor<ShutdownListener> captor = ArgumentCaptor.forClass(ShutdownListener.class); doNothing().when(mockShutdownWrapper).addShutdownListener(captor.capture()); doAnswer(new Answer() { public Object answer(InvocationOnMock invocationOnMock) throws Throwable { captor.getValue().shutdown(); return null; } }).when(mockRunnable).run(); monitor.watchThread(immediateExecutor(mockFuture), mockRunnable); verify(mockFuture).get(); }