DirContextPoolableObjectFactory extends BaseKeyedPoolableObjectFactory { public void destroyObject(Object key, Object obj) throws Exception { Assert.isTrue(obj instanceof DirContext, "The Object to validate must be of type '" + DirContext.class + "'"); try { final DirContext dirContext = (DirContext) obj; if (this.logger.isDebugEnabled()) { this.logger.debug("Closing " + key + " DirContext='" + dirContext + "'"); } dirContext.close(); if (this.logger.isDebugEnabled()) { this.logger.debug("Closed " + key + " DirContext='" + dirContext + "'"); } } catch (Exception e) { this.logger.warn( "An exception occured while closing '" + obj + "'", e); } }  ContextSource getContextSource(); void setContextSource(ContextSource contextSource); DirContextValidator getDirContextValidator(); void setDirContextValidator(DirContextValidator dirContextValidator); Object makeObject(Object key); boolean validateObject(Object key, Object obj); void destroyObject(Object key, Object obj);  }
@Test public void testDestroyObject() throws Exception { final DirContextPoolableObjectFactory objectFactory = new DirContextPoolableObjectFactory(); objectFactory.destroyObject(DirContextType.READ_ONLY, dirContextMock); DirContext throwingDirContextMock = Mockito.mock(DirContext.class); doThrow(new RuntimeException("Failed to close")) .when(throwingDirContextMock).close(); objectFactory.destroyObject(DirContextType.READ_ONLY, throwingDirContextMock); verify(dirContextMock).close(); }