Mah*_*leh 0 junit spring shiro
我有一个junit测试用例,它运行如下代码:
if (SecurityUtil.isAuthenticated()) {
}
Run Code Online (Sandbox Code Playgroud)
它给出了一个例外:
org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration.
Run Code Online (Sandbox Code Playgroud)
我的测试类配置如下:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ WebContextTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
@ActiveProfiles("test")
@DirtiesContext
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {
SpringConfig.class, SpringTestingConfig.class,
SpringLocalContainerJPAConfig.class, CustomConfiguration.class })
public class MyTestClass { }
Run Code Online (Sandbox Code Playgroud)
请告知如何修复此错误,谢谢.
解决如下:
private ThreadState _threadState;
protected Subject _mockSubject;
@Before
public void before() {
_mockSubject = Mockito.mock(Subject.class);
_threadState = new SubjectThreadState(_mockSubject);
_threadState.bind();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4329 次 |
| 最近记录: |