You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by Les Hazlewood <lh...@apache.org> on 2010/11/23 01:59:04 UTC

Testing with Shiro

Hi all,

I whipped this up today:

http://shiro.apache.org/testing.html

Feedback is welcome.

Cheers,

-- 
Les Hazlewood
Founder, Katasoft, Inc.
Application Security Products & Professional Apache Shiro Support and Training:
http://www.katasoft.com

Re: Testing with Shiro

Posted by Les Hazlewood <lh...@apache.org>.
Good idea.  This is definitely useful when you may not want to
initialize Shiro's SecurityManager.  For example, perhaps your Shiro
config uses an LDAP or rdbms Realm and you don't want to connect to
either when testing.

I'll add this to the documentation - thanks for sharing!

Regards,

Les

On Tue, Nov 23, 2010 at 9:39 AM, mbrictson <ma...@55minutes.com> wrote:
>
> 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.
>

Re: Testing with Shiro

Posted by mbrictson <ma...@55minutes.com>.
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.