大家好我正试图从Shiro包中模拟静态方法
org.apache.shiro.mgt.SecurityManager securityManager = EasyMock.createNiceMock(org.apache.shiro.mgt.SecurityManager.class);
SecurityUtils.setSecurityManager(securityManager);
PowerMock.mockStatic(SecurityUtils.class);
EasyMock.expect(SecurityUtils.getSubject()).andReturn(null);
PowerMock.replayAll(SecurityUtils.class);
PowerMock.verify(SecurityUtils.class);
Subject s = SecurityUtils.getSubject(); // this call returns null
s = SecurityUtils.getSubject(); // this call fails with exception below
所以我的想法是,当我在我的代码中调用getSubject时,我返回null(我可能会将其更改为实际对象,但是现在这可以工作。
目前的问题是我得到了异常
Usage is: expect(a.foo()).andXXX()
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:42)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
at $Proxy10.createSubject(Unknown Source)
at org.apache.shiro.subject.Subject$Builder.buildSubject(Subject.java:846)
at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56)
at com.mypackage.services.logic.AbstractHibernateTest.abstractBefore(AbstractHibernateTest.java:94)
null
读到这个解决方案应该是回复方法,但它不起作用。有没有人见过这个,或者有没有人有任何建议如何在Shiro SecurityUtils中模拟静态方法?
干杯
答案 0 :(得分:0)
这解决了我的问题
SecurityUtils.setSecurityManager(EasyMock.createNiceMock(org.apache.shiro.mgt.SecurityManager.class));
mockSubject = Mockito.mock(Subject.class);
threadState = new SubjectThreadState(mockSubject);
threadState.bind();