### Question:
CacheUtils { public void evictSubset(CacheManager cacheManager, String cacheName, Predicate<CompositeKey> keyPredicate) { final Cache cache = cacheManager.getCache(cacheName); if (cache == null) throw new IllegalArgumentException("Unknown cache " + cacheName); final Object nativeCache = cache.getNativeCache(); if (nativeCache instanceof net.sf.ehcache.Cache) { final net.sf.ehcache.Cache ehCache = (net.sf.ehcache.Cache) nativeCache; for (final Object key : ehCache.getKeys()) { if (key instanceof CompositeKey) { final CompositeKey compositeKey = ((CompositeKey) key); if (!keyPredicate.test(compositeKey)) continue; } ehCache.remove(key); } } else { cache.clear(); } }  void evictSubset(CacheManager cacheManager, String cacheName, Predicate<CompositeKey> keyPredicate); List<T> getAllRecords(CacheManager cacheManager, String cacheName);  }### Answer:
@Test public void testEvictSubset() { CacheManager cacheManager = mock(CacheManager.class); Cache cache1 = mock(Cache.class); when(cacheManager.getCache("cache1")).thenReturn(cache1); when(cache1.getNativeCache()).thenReturn(Object.class); Cache cache2 = mock(Cache.class); when(cacheManager.getCache("cache2")).thenReturn(cache2); net.sf.ehcache.Cache ehCache2 = PowerMockito.mock(net.sf.ehcache.Cache.class); when(cache2.getNativeCache()).thenReturn(ehCache2); List<Object> keys = Arrays.asList( "abc", new CompositeKey("test", 5), new CompositeKey("next", 10), new CompositeKey("next", 15) ); when(ehCache2.getKeys()).thenReturn(keys); try { underTest.evictSubset(cacheManager, "missing", x -> true); fail(); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains("Unknown cache")); assertTrue(e.getMessage().contains("missing")); } verify(cache1, never()).clear(); underTest.evictSubset(cacheManager, "cache1", x -> false); verify(cache1, times(1)).clear(); final Answer<Boolean> answer = invocation -> { removeCounter++; return true; }; doAnswer(answer).when(ehCache2).remove(any(Serializable.class)); doAnswer(answer).when(ehCache2).remove((Object) any()); assertEquals(0, removeCounter); underTest.evictSubset(cacheManager, "cache2", x -> false); assertEquals(1, removeCounter); verify(ehCache2, times(1)).remove(keys.get(0)); underTest.evictSubset(cacheManager, "cache2", x -> x.equals(0, "test")); assertEquals(3, removeCounter); verify(ehCache2, times(2)).remove(keys.get(0)); verify(ehCache2, times(1)).remove(keys.get(1)); underTest.evictSubset(cacheManager, "cache2", x -> (Integer) x.get(1) > 10); assertEquals(5, removeCounter); verify(ehCache2, times(3)).remove(keys.get(0)); verify(ehCache2, times(1)).remove(keys.get(3)); }