ToInstantiateInterceptorHandler implements InterceptorHandler { public void execute(InterceptorStack stack, ResourceMethod method, Object resourceInstance) throws InterceptionException { Interceptor interceptor = container.instanceFor(type); if (interceptor == null) { throw new InterceptionException("Unable to instantiate interceptor for " + type.getName() + ": the container returned null."); } if (interceptor.accepts(method)) { logger.debug("Invoking interceptor {}", interceptor.getClass().getSimpleName()); interceptor.intercept(stack, method, resourceInstance); } else { stack.next(method, resourceInstance); } } ToInstantiateInterceptorHandler(Container container, Class<? extends Interceptor> type); void execute(InterceptorStack stack, ResourceMethod method, Object resourceInstance); @Override String toString();  }
@Test(expected = InterceptionException.class) public void shouldComplainWhenUnableToInstantiateAnInterceptor() throws InterceptionException, IOException { when(container.instanceFor(MyWeirdInterceptor.class)).thenReturn(null); ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, MyWeirdInterceptor.class); handler.execute(null, null, null); }
@Test public void shouldInvokeInterceptorsMethodIfAbleToInstantiateIt() throws InterceptionException, IOException { final Object instance = new Object(); when(container.instanceFor(Interceptor.class)).thenReturn(interceptor); when(interceptor.accepts(method)).thenReturn(true); ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, Interceptor.class); handler.execute(stack, method, instance); verify(interceptor).intercept(stack, method, instance); }
@Test public void shouldNotInvokeInterceptorsMethodIfInterceptorDoesntAcceptsResource() throws InterceptionException, IOException { final Object instance = new Object(); when(container.instanceFor(Interceptor.class)).thenReturn(interceptor); when(interceptor.accepts(method)).thenReturn(false); ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, Interceptor.class); handler.execute(stack, method, instance); verify(interceptor, never()).intercept(stack, method, instance); verify(stack).next(method, instance); }