CompositeDiskCache implements DiskCache { @Override public File get(String key) { synchronized (this) { for (int i=0; i<leafs.size(); i++) { DiskCache cache = leafs.get(i); File file = cache.get(key); if (file != null) { return file; } } } return null; } CompositeDiskCache(DiskCache... caches); @Override File get(String key); @Override void put(String key, Writer writer); @Override void delete(String key); @Override void clear();  }
@Test public void testCompositeGetsToAllCaches() { DiskCache cache1 = Mockito.mock(DiskCache.class); DiskCache cache2 = Mockito.mock(DiskCache.class); DiskCache cache3 = Mockito.mock(DiskCache.class); CompositeDiskCache cache = new CompositeDiskCache(cache1, cache2, cache3); cache.get(""); Mockito.verify(cache1, Mockito.times(1)).get(Mockito.anyString()); Mockito.verify(cache2, Mockito.times(1)).get(Mockito.anyString()); Mockito.verify(cache3, Mockito.times(1)).get(Mockito.anyString()); }
@Test public void testDoesGetStopsAfterFind() { DiskCache cache1 = Mockito.mock(DiskCache.class); DiskCache cache2 = Mockito.mock(DiskCache.class); DiskCache cache3 = Mockito.mock(DiskCache.class); DiskCache cache4 = Mockito.mock(DiskCache.class); Mockito.when(cache1.get(Mockito.anyString())).thenReturn(getContext().getFilesDir()); CompositeDiskCache cache = new CompositeDiskCache(cache1, cache2, cache3, cache4); File file = cache.get("anything"); Assert.assertNotNull(file); Mockito.verify(cache1, Mockito.times(1)).get(Mockito.anyString()); Mockito.verify(cache2, Mockito.never()).get(Mockito.anyString()); Mockito.verify(cache3, Mockito.never()).get(Mockito.anyString()); Mockito.verify(cache4, Mockito.never()).get(Mockito.anyString()); }