OptionalTypeFixtureConverter implements FixtureConverter { @Override public Object convert(FixtureTemplate fixtureTemplate, TypeToken typeToken) { if (typeToken.getRawType() != Optional.class) { return fixtureConverter.convert(fixtureTemplate, typeToken); } List<TypeToken<?>> types = TypeTokenUtils.extractGenericTypeTokens(typeToken); if (types.isEmpty()) { if (fixtureTemplate instanceof FixtureValue) { Object value = ((FixtureValue) fixtureTemplate).getValue(); return Optional.of(value); } return null; } else { return Optional.of(fixtureConverter.convert(fixtureTemplate, types.get(0))); } } OptionalTypeFixtureConverter(FixtureConverter converter); @Override Object convert(FixtureTemplate fixtureTemplate, TypeToken typeToken);  }
@Test public void testOptionalMapping() { Map<String, Object> map = new HashMap<>(); map.put("simple", 1); map.put("optionalString", "testString"); Map<String, String> beanMap = new HashMap<>(); beanMap.put("name", "testName"); map.put("optionalBean", beanMap); FixtureTemplate fixture = FixtureTemplateWrapper.wrap(map, null, null); OptionalTest result = (OptionalTest) converter.convert(fixture, TypeToken.of(OptionalTest.class)); assertEquals(1, result.simple.get()); assertEquals("testString", result.optionalString.get()); assertEquals("testName", result.optionalBean.get().name.get()); }
@Test public void testOptionalMapping() { Map map = new HashMap(); map.put("simple", 1); map.put("optionalString", "testString"); Map beanMap = new HashMap(); beanMap.put("name", "testName"); map.put("optionalBean", beanMap); FixtureTemplate fixture = FixtureTemplateWrapper.wrap(map, null, null); OptionalTest result = (OptionalTest) converter.convert(fixture, TypeToken.of(OptionalTest.class)); assertEquals(1, result.simple.get()); assertEquals("testString", result.optionalString.get()); assertEquals("testName", result.optionalBean.get().name.get()); }