在我的项目中,shiro会话用于对用户进行身份验证。我将为服务调用编写一个模拟测试。即
object.setCreatedBy(SecurityUtils.getSubject()。getPrincipal()。的toString())
。现在我要填充从测试用例这个值(它设置的loggedIn用户(如砂质)在CreatedBy字段使用JUnit 4.0和3.0方便)。我现在用下面的代码如何嘲笑shirosession?
public class ExampleShiroUnitTest extends AbstractShiroTest {
@Test
public void testSimple() {
//1. Create a mock authenticated Subject instance for the test to run:
Subject subjectUnderTest = createNiceMock(Subject.class);
expect(subjectUnderTest.isAuthenticated()).andReturn(true);
//2. Bind the subject to the current thread:
setSubject(subjectUnderTest);
}
@After
public void tearDownSubject() {
//3. Unbind the subject from the current thread:
clearSubject();
}
}
在http://shiro.apache.org/testing.html给出。 在上面的方法中,主题设置正确,并给出适当的值时,但不知道如何得到本金出于此。当我从主题访问它时,它返回null,并且没有setPrincipal()方法。
提及AbstractShiroTest的+1。我为SecurityUtils创建了一个非静态包装器,所以我可以模拟并注入它,但是当我开始时,这会很好。 – jhericks 2012-07-11 22:07:30