You must add the SecurityContext to the context data map in the 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()); } }
eiden source share