@Before public void bindSubjectToThread() { // setup a simple realm for authc SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm(); simpleAccountRealm.addAccount("anonymous", "anonymous"); DefaultSecurityManager securityManager = new DefaultSecurityManager(); securityManager.setRealm(simpleAccountRealm); DefaultSessionManager sessionManager = (DefaultSessionManager) securityManager.getSessionManager(); sessionDAO = new EnterpriseCacheSessionDAO(); sessionManager.setSessionDAO(sessionDAO); simpleSession = new SimpleSession(); sessionDAO.create(simpleSession); List<PrincipalCollection> principalCollectionList = new ArrayList<PrincipalCollection>(); principalCollectionList.add(new SimplePrincipalCollection("other Principal", "some-realm")); simpleSession.setAttribute( DelegatingSubject.class.getName() + ".RUN_AS_PRINCIPALS_SESSION_KEY", principalCollectionList); DelegatingSession delegatingSession = new DelegatingSession(sessionManager, new DefaultSessionKey(simpleSession.getId())); // set the user subject = new DelegatingSubject( new SimplePrincipalCollection("anonymous", "realmName"), true, null, delegatingSession, securityManager); ThreadContext.bind(subject); }