GlobalContext { public void switchUser(final User user, final Runnable taskRunnable) { ADVISORY_SERVICE_LOGGER.info("Switching user to: " + (user == null ? "null" : user.getUserId())); if (taskRunnable != null) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { userProfile.setUser(user); taskRunnable.run(); } }); } else { userProfile.setUser(user); } } private  GlobalContext(); static GlobalContext getGlobalContext(); User getUser(); void switchUser(final User user, final Runnable taskRunnable); IIdentityManager getIdManager(); void setIdManager(IIdentityManager idManager);  }
@Test public void switchUserTest() { final CyclicBarrier barrier = new CyclicBarrier(2); final AtomicBoolean runnableRan = new AtomicBoolean(false); final Runnable testRunnable = new Runnable() { @Override public void run() { try { runnableRan.set(true); barrier.await(); } catch (InterruptedException e) { throw new AssertionError(e); } catch (BrokenBarrierException e) { throw new AssertionError(e); } } }; GlobalContext.getGlobalContext().switchUser(user1, null); assertSame(GlobalContext.getGlobalContext().getUser(), user1); Thread t = new Thread(new Runnable() { @Override public void run() { GlobalContext.getGlobalContext().switchUser(user2, testRunnable); } }); t.start(); try { barrier.await(); } catch (InterruptedException e) { throw new AssertionError(e); } catch (BrokenBarrierException e) { throw new AssertionError(e); } assertTrue(runnableRan.get()); }