### Question:
Dispatcher { public boolean dispatch() throws InterruptedException { logger.info("Pre workerProvider.get()"); Worker worker = workerProvider.get(); logger.info("Post workerProvider.get()"); if (worker == ExitWorkerProvider.EXIT_SIGNAL) { return false; } logger.info("About to submit work to executor"); executor.getQueue().put(worker); logger.info("Submitted work"); return true; }  boolean dispatch();  }### Answer:
@Test public void dispatch_workExists_workDispatched() throws Exception { Worker workerMock = createMock(Worker.class); expect(workerProviderMock.get()).andReturn(workerMock); BlockingQueue<Runnable> queueMock = createMock(BlockingQueue.class); queueMock.put(anyObject(Runnable.class)); expectLastCall().once(); expect(executorServiceMock.getQueue()).andReturn(queueMock).once(); replay(workerMock, workerProviderMock, executorServiceMock, queueMock); boolean retval = this.dispatcher.dispatch(); verify(workerMock, workerProviderMock, executorServiceMock, queueMock); assertTrue(retval); }
@Test public void dispatch_exitWorkerProvided_dispatchExitsWithFalse() throws Exception { expect(workerProviderMock.get()).andReturn(ExitWorkerProvider.EXIT_SIGNAL); replay(workerProviderMock, executorServiceMock); boolean retval = dispatcher.dispatch(); verify(workerProviderMock, executorServiceMock); assertFalse(retval); }