### Question:
ResourcesFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { openHttpClient(); chain.doFilter(request, response); } finally { opushResourcesHolder.close(); } } @Inject @VisibleForTesting  ResourcesFilter(OpushResourcesHolder opushResourcesHolder); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }### Answer:
@Test public void filterShouldCloseResourcesWhenChainIsDone() throws Exception { chain.doFilter(request, response); expectLastCall(); opushResourcesHolder.put(same(HttpClientResource.class), isA(HttpClientResource.class)); expectLastCall(); opushResourcesHolder.close(); expectLastCall(); mocks.replay(); new ResourcesFilter(opushResourcesHolder).doFilter(request, response, chain); mocks.verify(); }
@Test(expected=IllegalStateException.class) public void filterShouldCloseResourcesWhenChainTriggersException() throws Exception { chain.doFilter(request, response); expectLastCall().andThrow(new IllegalStateException("error")); opushResourcesHolder.put(same(HttpClientResource.class), isA(HttpClientResource.class)); expectLastCall(); opushResourcesHolder.close(); expectLastCall(); mocks.replay(); try { new ResourcesFilter(opushResourcesHolder).doFilter(request, response, chain); } catch (Exception e) { mocks.verify(); throw e; } }