HashMapCache implements ICache<K, V> { @Override public void put(final K key, final V value) { if (useJavaSerialisation) { try { cache.put(key, JAVA_SERIALISER.serialise(value)); } catch (final SerialisationException e) { throw new RuntimeException(e); } } else { cache.<K, V>put(key, value); } } HashMapCache(final boolean useJavaSerialisation);  HashMapCache(); @Override V get(final K key); @Override void put(final K key, final V value); @Override void remove(final K key); @Override Collection<V> getAllValues(); @Override Set<K> getAllKeys(); @Override int size(); @Override void clear();  }
@DisplayName("Should cause JavaSerialisableException when serialisation flag is true") @Test public void shouldThrowRuntimeExceptionCausedByNonJavaSerialisableException() { final HashMapCache<String, Object> map = new HashMapCache<>(true); final String s = "hello"; map.put("test1", s); class TempClass { } TempClass tempClass = new TempClass(); final Exception exception = assertThrows(RuntimeException.class, () -> map.put("test1", tempClass)); assertTrue(exception.getCause() instanceof SerialisationException); }
@DisplayName("Should not cause JavaSerialisableException when serialisation flag is false") @Test public void shouldNotThrowAnyExceptions() { final HashMapCache<String, Object> map = new HashMapCache<>(false); map.put("test1", "hello"); class TempClass { } final TempClass tempClass = new TempClass(); map.put("test1", tempClass); }