CacheManagerImpl implements CacheManager { @Override public void dispose() { for (Map.Entry<String, Object> entry : cache.entrySet()) { if (entry.getValue() instanceof Cacheable) { ((Cacheable) entry.getValue()).close(); } } cache.clear(); }  @Override void add(String key, Object value); @Override Object get(String key); @Override Object remove(String key); @Override void dispose();  }
@Test public void testDispose() throws Exception { CacheManager cacheManager = new CacheManagerImpl(); Cacheable cacheable = mock(Cacheable.class); Cacheable otherCacheable = mock(Cacheable.class); Object cached = new Object(); cacheManager.add("cacheable", cacheable); cacheManager.add("other_cacheable", otherCacheable); cacheManager.add("cached", cached); assertEquals(cacheable, cacheManager.get("cacheable")); assertEquals(otherCacheable, cacheManager.get("other_cacheable")); assertEquals(cached, cacheManager.get("cached")); cacheManager.dispose(); assertNull(cacheManager.get("cacheable")); assertNull(cacheManager.get("other_cacheable")); assertNull(cacheManager.get("cached")); verify(cacheable).close(); verify(otherCacheable).close(); }