### Question:
CacheUtils { public <T> List<T> getAllRecords(CacheManager cacheManager, String cacheName) { 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; return (List<T>) ehCache.getAll(ehCache.getKeys()) .values() .stream() .map(Element::getObjectValue) .collect(Collectors.toList()); } else { throw new IllegalArgumentException("Unsupported type of cache : " + nativeCache.getClass()); } }  void evictSubset(CacheManager cacheManager, String cacheName, Predicate<CompositeKey> keyPredicate); List<T> getAllRecords(CacheManager cacheManager, String cacheName);  }### Answer:
@Test public void givenUnknownCacheName_whenGetAllRecords_thenThrowsException() { CacheManager cacheManager = mock(CacheManager.class); IllegalArgumentException iae = Assertions.assertThrows( IllegalArgumentException.class, () -> underTest.getAllRecords(cacheManager, "unknownCacheName") ); assertEquals("Unknown cache unknownCacheName", iae.getMessage()); }
@Test public void givenUnsupportedCacheManager_whenGetAllRecords_thenThrowsException() { CacheManager cacheManager = mock(CacheManager.class); Cache cache = mock(Cache.class); when(cacheManager.getCache("knownCacheName")).thenReturn(cache); when(cache.getNativeCache()).thenReturn(new Object()); IllegalArgumentException iae = Assertions.assertThrows( IllegalArgumentException.class, () -> underTest.getAllRecords(cacheManager, "knownCacheName") ); assertTrue(iae.getMessage().startsWith("Unsupported type of cache : ")); }
@Test public void givenValidCacheManager_whenGetAllRecords_thenReadAllStoredRecords() { CacheManager cacheManager = mock(CacheManager.class); Cache cache = mock(Cache.class); net.sf.ehcache.Cache ehCache = PowerMockito.mock(net.sf.ehcache.Cache.class); Map<Integer, String> entries = new HashMap<>(); entries.put(1, "a"); entries.put(2, "b"); entries.put(3, "c"); List<Object> keys = new ArrayList<>(entries.keySet()); when(cacheManager.getCache("knownCacheName")).thenReturn(cache); when(cache.getNativeCache()).thenReturn(ehCache); when(ehCache.getKeys()).thenReturn(keys); when(ehCache.getAll(keys)).thenReturn(convert(entries)); Collection<String> values = underTest.getAllRecords(cacheManager, "knownCacheName"); assertNotNull(values); assertEquals(3, values.size()); assertTrue(values.contains("a")); assertTrue(values.contains("b")); assertTrue(values.contains("c")); }