4

RestEasyを使用してRESTサーバーを開発し、org.jboss.resteasy.mockMockDispatcherFactory単体テストでサービスをテストするためにモックディスパッチャー()を使用しています。私のサービスにはダイジェスト認証が必要であり、それをテストの一部にします。

私の各サービスはパラメータを受け入れ@Context SecurityContext securityContextます。

SecurityContextセキュリティメソッドが正しく機能することをテストできるように、ディスパッチャに偽物を挿入する方法はありますか?

4

2 に答える 2

3

SecurityContextのコンテキスト データ マップにを追加する必要がありますResteasyProviderFactory

public class SecurityContextTest  {

    @Path("/")
    public static class Service {
        @Context
        SecurityContext context;

        @GET
        public String get(){
            return context.getAuthenticationScheme();
        }
    }

    public static class FakeSecurityContext extends ServletSecurityContext {

        public FakeSecurityContext() {
            super(null);
        }

        @Override
        public String getAuthenticationScheme() {
            return "unit-test-scheme";
        }
    }

    @Test
    public void securityContextTest() throws Exception {
        Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getRegistry().addSingletonResource(new Service());
        ResteasyProviderFactory.getContextDataMap().put(SecurityContext.class, new FakeSecurityContext());

        MockHttpRequest request = MockHttpRequest.get("/");
        MockHttpResponse response = new MockHttpResponse();

        dispatcher.invoke(request, response);

        assertEquals("unit-test-scheme", response.getContentAsString());
    }
}
于 2012-07-07T15:11:32.033 に答える