EventBufferSingleThreadAccessMock implements ISingleThreadAccess { public void checkThread() { if (!isSingleThread()) { throw new IllegalStateException("Must be invoked in single thread"); } } EventBufferSingleThreadAccessMock(); @Override boolean isSingleThread(); @Override void invoke(final Runnable runnable); int getEventsCount(); List<Runnable> getEventsCopy(); List<Runnable> popEvents(); void runEvents(); void checkThread();  }
@Test public void testCheckThreadPositive() { threadAccess.checkThread(); }
@Test(expected = IllegalStateException.class) public void testCheckThreadNegative() { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<EventBufferSingleThreadAccessMock> threadAcccessReference = new AtomicReference<EventBufferSingleThreadAccessMock>(); new Thread(new Runnable() { @Override public void run() { threadAcccessReference.set(new EventBufferSingleThreadAccessMock()); latch.countDown(); } }).start(); try { latch.await(); } catch (final InterruptedException e) { throw new RuntimeException(e); } threadAcccessReference.get().checkThread(); }