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 <m...@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.
>

Reply via email to