私のプロジェクトでは、shiroセッションを使用してユーザーを認証します。サービスコールの模擬テストを作成します。つまり
、object.setCreatedBy(SecurityUtils.getSubject()。getPrincipal()。toString())
は、ログインしたユーザー(sandyなど)をCreatedByフィールドに設定します。次に、この値をtestCaseから入力します(Junit4.0およびeasy3.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()としてのメソッドがありません。