### Question:
MemcachedItemCache implements ItemCache { @Override public Object getItem(final String key, final long timeout) { Object item = null; final Future<Object> f = memcachedClient.asyncGet(key); try { item = f.get(timeout, TimeUnit.SECONDS); } catch (final TimeoutException e) { f.cancel(false); logger.trace("Unable to get cache item within given time", e); return null; } catch (final InterruptedException | ExecutionException e) { logger.debug("Unable to get cache item", e); } return item; } MemcachedItemCache(final MemcachedClient memcachedClient); @Override Object getItem(final String key, final long timeout); @Override void putItem(final String key, final Object item, final long expire);  }### Answer:
@SuppressWarnings("unchecked") @Test public void shouldGetItem() throws InterruptedException, TimeoutException, ExecutionException { final String key = Randoms.randomString(); final String item = Randoms.randomString(); final int timeout = Randoms.randomInt(5) + 1; final GetFuture<Object> f = mock(GetFuture.class); when(memcachedClient.asyncGet(key)).thenReturn(f); when(f.get(timeout, TimeUnit.SECONDS)).thenReturn(item); final Object obj = memcachedItemCache.getItem(key, timeout); assertEquals(item, obj); }
@SuppressWarnings("unchecked") @Test public void shouldNotGetItem_throwsTimeout() throws InterruptedException, ExecutionException, TimeoutException { final String key = Randoms.randomString(); final int timeout = Randoms.randomInt(5) + 1; final GetFuture<Object> f = mock(GetFuture.class); when(memcachedClient.asyncGet(key)).thenReturn(f); when(f.get(timeout, TimeUnit.SECONDS)).thenThrow(TimeoutException.class); final Object item = memcachedItemCache.getItem(key, timeout); assertNull(item); }