在我的项目中,shiro 会话用于对用户进行身份验证。我将为服务调用编写一个模拟测试。即
object.setCreatedBy(SecurityUtils.getSubject().getPrincipal().toString()
) 在 CreatedBy 字段
中设置登录用户(例如sandy)。现在我想从 testCase 填充这个值(使用 Junit 4.0 和 easy 3.0)。我正在使用以下代码
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() 方法。