### Question:
KMSCacheKeyProvider extends KeyProvider { @Override public KeyVersion getKeyVersion(String versionName) throws IOException { try { return keyVersionCache.get(versionName); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof KeyNotFoundException) { return null; } else if (cause instanceof IOException) { throw (IOException) cause; } else { throw new IOException(cause); } } } KMSCacheKeyProvider(KeyProvider prov, long timeoutMillis); @Override KeyVersion getCurrentKey(String name); @Override KeyVersion getKeyVersion(String versionName); @Override List<String> getKeys(); @Override List<KeyVersion> getKeyVersions(String name); @Override Metadata getMetadata(String name); @Override KeyVersion createKey(String name, byte[] material,
      Options options); @Override KeyVersion createKey(String name,
      Options options); @Override void deleteKey(String name); @Override KeyVersion rollNewVersion(String name, byte[] material); @Override KeyVersion rollNewVersion(String name); @Override void flush(); @Override Metadata[] getKeysMetadata(String ... keyNames); @Override boolean isTransient();  }### Answer:
@Test public void testKeyVersion() throws Exception { KeyProvider.KeyVersion mockKey = Mockito.mock(KeyProvider.KeyVersion.class); KeyProvider mockProv = Mockito.mock(KeyProvider.class); Mockito.when(mockProv.getKeyVersion(Mockito.eq("k1@0"))).thenReturn(mockKey); Mockito.when(mockProv.getKeyVersion(Mockito.eq("k2@0"))).thenReturn(null); KeyProvider cache = new KMSCacheKeyProvider(mockProv, 100); Assert.assertEquals(mockKey, cache.getKeyVersion("k1@0")); Mockito.verify(mockProv, Mockito.times(1)).getKeyVersion(Mockito.eq("k1@0")); Assert.assertEquals(mockKey, cache.getKeyVersion("k1@0")); Mockito.verify(mockProv, Mockito.times(1)).getKeyVersion(Mockito.eq("k1@0")); Thread.sleep(200); Assert.assertEquals(mockKey, cache.getKeyVersion("k1@0")); Mockito.verify(mockProv, Mockito.times(2)).getKeyVersion(Mockito.eq("k1@0")); cache = new KMSCacheKeyProvider(mockProv, 100); Assert.assertEquals(null, cache.getKeyVersion("k2@0")); Mockito.verify(mockProv, Mockito.times(1)).getKeyVersion(Mockito.eq("k2@0")); Assert.assertEquals(null, cache.getKeyVersion("k2@0")); Mockito.verify(mockProv, Mockito.times(2)).getKeyVersion(Mockito.eq("k2@0")); }