/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package org.springside.modules.test.security.shiro; import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.support.SubjectThreadState; import org.apache.shiro.util.ThreadState; import org.mockito.Mockito; /** * 在单元测试中的Shiro工具类, * * @author calvin */ public class ShiroTestUtils { private static ThreadState threadState; /** * 用Mockito快速創建一個已認證的用户. */ public static void mockSubject(Object principal) { Subject subject = Mockito.mock(Subject.class); Mockito.when(subject.isAuthenticated()).thenReturn(true); Mockito.when(subject.getPrincipal()).thenReturn(principal); bindSubject(subject); } /** * 綁定Subject到當前線程. */ protected static void bindSubject(Subject subject) { clearSubject(); threadState = new SubjectThreadState(subject); threadState.bind(); } /** * 清除當前線程中的Subject. */ public static void clearSubject() { if (threadState != null) { threadState.clear(); threadState = null; } } }