@Test public void shouldCacheResultsForSubsequentQueries() throws Exception { when(delegate.forName("hej")).thenReturn(lookup, lookup2); Lookup first = factory.forName("hej"); Lookup second = factory.forName("hej"); assertThat(first == second, is(true)); }
@Test public void shouldReturnDifferentForDifferentQueries() throws Exception { when(delegate.forName("hej")).thenReturn(lookup); when(delegate.forName("hopp")).thenReturn(lookup2); Lookup first = factory.forName("hej"); Lookup second = factory.forName("hopp"); assertThat(first == second, is(false)); }
@Test public void shouldReturnDifferentForDifferentThreads() throws Exception { ExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); factory = new CachingLookupFactory(new SimpleLookupFactory()); Lookup first = factory.forName("hej"); Lookup second = executorService .submit( new Callable<Lookup>() { @Override public Lookup call() throws Exception { return factory.forName("hej"); } }) .get(); assertThat(second, not(equalTo(first))); }
@Test public void shouldReturnResultsFromDelegate() throws Exception { when(delegate.forName("a name")).thenReturn(lookup); assertThat(factory.forName("a name"), equalTo(lookup)); }