### Question:
GlusterWatchService implements WatchService { @Override public WatchKey take() { while (running) { WatchKey key = poll(); if (key != null) { return key; } try { Thread.sleep(PERIOD); } catch (InterruptedException e) { } } throw new ClosedWatchServiceException(); }  WatchKey registerPath(GlusterPath path, WatchEvent.Kind... kinds); @Override void close(); @Override WatchKey poll(); @Override WatchKey poll(long timeout, TimeUnit unit); @Override WatchKey take(); static final int MILLIS_PER_SECOND; static final int MILLIS_PER_MINUTE; static final int MILLIS_PER_HOUR; static final int MILLIS_PER_DAY; static long PERIOD; }### Answer:
@Test(expected = ClosedWatchServiceException.class) public void testTake_whenClosed() { watchService.setRunning(false); watchService.take(); }
@Test public void testTake() throws Exception { WatchKey mockKey = mock(WatchKey.class); doReturn(null).doReturn(mockKey).when(watchService).poll(); PowerMockito.spy(Thread.class); PowerMockito.doThrow(new InterruptedException()).when(Thread.class); Thread.sleep(GlusterWatchService.PERIOD); WatchKey key = watchService.take(); assertEquals(mockKey, key); Mockito.verify(watchService, Mockito.times(2)).poll(); PowerMockito.verifyStatic(); Thread.sleep(GlusterWatchService.PERIOD); }