I have had success with a simpler testing approach that mocks the Shiro
Subject. Here is my base test class:


import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.SubjectThreadState;
import org.apache.shiro.util.ThreadState;
import org.junit.After;
import org.junit.Before;
import org.mockito.Mockito;

/**
 * Provides support for mocking Shiro's Subject to allow unit testing of
 * classes that interact with Shiro's {...@code SecurityUtils.getSubject()}.
 */
public abstract class BaseUnitTest
{
    private ThreadState _threadState;
    protected Subject _mockSubject;
    
    @Before
    public void attachSubject()
    {
        _mockSubject = Mockito.mock(Subject.class);
        _threadState = new SubjectThreadState(_mockSubject);
        _threadState.bind();
    }
    
    @After
    public void detachSubject()
    {
        _threadState.clear();
    }
}


Then my actual tests can do things like this:


// Simulate authenticated subject
Mockito.when(_mockSubject.isAuthenticated()).thenReturn(true);

-- 
View this message in context: 
http://shiro-user.582556.n2.nabble.com/Testing-with-Shiro-tp5765075p5767543.html
Sent from the Shiro User mailing list archive at Nabble.com.

Reply via email to