1

在我的项目中,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() 方法。

4

1 回答 1

2

我可能误解了你的问题,但这样的事情不能解决问题吗?

expect(subjectUnderTest.getPrincipal()).andReturn("sandy");
于 2012-07-11T19:21:31.393 回答