title: Ehcache hangs in test
body: <p>I am in the process of rewriting a bottle neck in the code of the project I am on, and in doing so I am creating a top level item that contains a self populating Ehcache. I am attempting to write a test to make sure that the basic call chain is established, but when the test executes it hands when retrieving the item from the cache.</p>
  
  <p>Here are the Setup and the test, for reference mocking is being done with Mockito:</p>
  
  <pre><code>@Before
  public void SetUp()
  {
      testCache = new Cache(getTestCacheConfiguration());
      recordingFactory = new EntryCreationRecordingCache();
      service = new Service&lt;Request, Response&gt;(testCache, recordingFactory); 
  }
  
  @Test
  public void retrievesResultsFromSuppliedCache()
  {
      ResultType resultType = mock(ResultType.class);
      Response expectedResponse = mock(Response.class);
      addToExpectedResults(resultType, expectedResponse);
      Request request = mock(Request.class);
      when(request.getResultType()).thenReturn(resultType);
  
      assertThat(service.getResponse(request), sameInstance(expectedResponse));
      assertTrue(recordingFactory.requestList.contains(request));
  }
  
  private void addToExpectedResults(ResultType resultType,
          Response response) {
      recordingFactory.responseMap.put(resultType, response);
  
  }
  
  private CacheConfiguration getTestCacheConfiguration() {
      CacheConfiguration cacheConfiguration = new CacheConfiguration("TEST_CACHE", 10);
      cacheConfiguration.setLoggingEnabled(false);
      return cacheConfiguration;
  }
  
  private class EntryCreationRecordingCache extends ResponseFactory{
  
      public final Map&lt;ResultType, Response&gt; responseMap = new ConcurrentHashMap&lt;ResultType, Response&gt;();
      public final List&lt;Request&gt; requestList = new ArrayList&lt;Request&gt;();
  
      @Override
      protected Map&lt;ResultType, Response&gt; generateResponse(Request request) {
          requestList.add(request);
          return responseMap;
      }
  }
  </code></pre>
  
  <p>Here is the ServiceClass</p>
  
  <pre><code>public class Service&lt;K extends Request, V extends Response&gt; {
  
      private Ehcache cache;
  
      public Service(Ehcache cache, ResponseFactory factory) {
          this.cache = new SelfPopulatingCache(cache, factory);
      }
  
      @SuppressWarnings("unchecked")
      public V getResponse(K request)
      {
          ResultType resultType = request.getResultType();
          Element cacheEntry = cache.get(request);
          V response = null;
          if(cacheEntry != null){
              Map&lt;ResultType, Response&gt; resultTypeMap = (Map&lt;ResultType, Response&gt;) cacheEntry.getValue();
              try{
                  response = (V) resultTypeMap.get(resultType);
              }catch(NullPointerException e){
                  throw new RuntimeException("Result type not found for Result Type: " + resultType);
              }catch(ClassCastException e){
                  throw new RuntimeException("Incorrect Response Type for Result Type: " + resultType);
              }
          }
          return response;
      }
  }
  </code></pre>
  
  <p>And here is the ResponseFactory:</p>
  
  <pre><code>public abstract class ResponseFactory implements CacheEntryFactory{
  
      @Override
      public final Object createEntry(Object request) throws Exception {
          return generateResponse((Request)request);
      }
  
      protected abstract Map&lt;ResultType,Response&gt; generateResponse(Request request);
  }
  </code></pre>