RobotsLoaderCachedImpl implements RobotsLoader { @Nonnull @Override public Robots load(@Nonnull final URI robotsResource) throws Exception { checkNotNull(robotsResource, "robotsResource"); return cache.get(robotsResource, new Callable<Robots>() { @Override public Robots call() throws Exception { return delegate.load(robotsResource); } }); } RobotsLoaderCachedImpl(
            @Nonnull final RobotsLoader delegate,
            @Nonnull final Cache<URI, Robots> cache); @Nonnull @Override Robots load(@Nonnull final URI robotsResource); @Override void close();  }
@Test(expected = NullPointerException.class) public void givenNullUri_whenLoad_thenThrowsNPE() throws Exception { loader.load(null); }
@Test public void givenValidUri_whenLoad_resultEqualsExpected() throws Exception { Robots expected = new Robots.Builder().build(); when(cache.get(eq(EXAMPLE_URI), anyCallableReturning(Robots.class))).thenReturn(expected); Robots result = loader.load(EXAMPLE_URI); assertThat(result, equalTo(expected)); }
@Test public void givenCacheMiss_whenLoad_resultInvokesDelegateOnce() throws Exception { when(cache.get(eq(EXAMPLE_URI), anyCallableReturning(Robots.class))).then(callArgument(1, Robots.class)); loader.load(EXAMPLE_URI); verify(delegate).load(EXAMPLE_URI); verifyNoMoreInteractions(delegate); }
@Test public void givenCacheHit_whenLoad_resultNotInvokesDelegate() throws Exception { when(cache.get(eq(EXAMPLE_URI), anyCallableReturning(Robots.class))).thenAnswer(new Answer<Robots>() { @Override public Robots answer(InvocationOnMock invocation) throws Throwable { return new Robots.Builder().build(); } }); loader.load(EXAMPLE_URI); verifyZeroInteractions(delegate); }