CachingConverter implements IConverter<C> { @Override public String convertToString(C value, Locale locale) { try { return toStringCache.get(new Key<>(value, locale)); } catch (ExecutionException ex) { throw new ConversionException(ex); } } CachingConverter(IConverter<C> converter); static IConverter<C> wrap(IConverter<C> inner); @Override C convertToObject(String value, Locale locale); @Override String convertToString(C value, Locale locale);  }
@Test public void testConvertToString() { final IConverter<Object> inner = context.mock(IConverter.class, "Object"); final CachingConverter instance = new CachingConverter<>(inner); final Object value = new Object(); final String result = "result"; context.checking(new Expectations() { { oneOf(inner).convertToString(value, Locale.PRC); will(returnValue(result)); } }); String actual = instance.convertToString(value, Locale.PRC); assertSame(result, actual); actual = instance.convertToString(value, Locale.PRC); assertSame(result, actual); actual = instance.convertToString(value, Locale.PRC); assertSame(result, actual); final Object value2 = new Object(); context.checking(new Expectations() { { oneOf(inner).convertToString(value, Locale.CHINA); will(returnValue(result)); oneOf(inner).convertToString(value2, Locale.CHINA); will(returnValue(result)); } }); actual = instance.convertToString(value, Locale.CHINA); assertSame(result, actual); actual = instance.convertToString(value2, Locale.CHINA); assertSame(result, actual); }