### Question:
MapPopulator { @SuppressWarnings("unchecked") Map<?, ?> getRandomMap(final Field field, final RandomizationContext context) { int randomSize = getRandomMapSize(context.getParameters()); Class<?> fieldType = field.getType(); Type fieldGenericType = field.getGenericType(); Map<Object, Object> map; if (isInterface(fieldType)) { map = (Map<Object, Object>) getEmptyImplementationForMapInterface(fieldType); } else { try { map = (Map<Object, Object>) fieldType.newInstance(); } catch (InstantiationException | IllegalAccessException e) { if (fieldType.isAssignableFrom(EnumMap.class)) { if (isParameterizedType(fieldGenericType)) { Type type = ((ParameterizedType) fieldGenericType).getActualTypeArguments()[0]; map = new EnumMap((Class<?>)type); } else { return null; } } else { map = (Map<Object, Object>) objectFactory.createInstance(fieldType, context); } } } if (isParameterizedType(fieldGenericType)) { ParameterizedType parameterizedType = (ParameterizedType) fieldGenericType; Type keyType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[1]; if (isPopulatable(keyType) && isPopulatable(valueType)) { for (int index = 0; index < randomSize; index++) { Object randomKey = easyRandom.doPopulateBean((Class<?>) keyType, context); Object randomValue = easyRandom.doPopulateBean((Class<?>) valueType, context); if(randomKey != null) { map.put(randomKey, randomValue); } } } } return map; } MapPopulator(final EasyRandom easyRandom, final ObjectFactory objectFactory);   }### Answer:
@Test void rawInterfaceMapTypesMustBeGeneratedEmpty() throws Exception { when(context.getParameters()).thenReturn(parameters); Field field = Foo.class.getDeclaredField("rawMap"); Map<?, ?> randomMap = mapPopulator.getRandomMap(field, context); assertThat(randomMap).isEmpty(); }
@Test void rawConcreteMapTypesMustBeGeneratedEmpty() throws Exception { when(context.getParameters()).thenReturn(parameters); Field field = Foo.class.getDeclaredField("concreteMap"); Map<?, ?> randomMap = mapPopulator.getRandomMap(field, context); assertThat(randomMap).isEmpty(); }
@Test void typedInterfaceMapTypesMightBePopulated() throws Exception { when(context.getParameters()).thenReturn(parameters); when(easyRandom.doPopulateBean(String.class, context)).thenReturn(FOO, BAR); Field field = Foo.class.getDeclaredField("typedMap"); Map<String, String> randomMap = (Map<String, String>) mapPopulator.getRandomMap(field, context); assertThat(randomMap).containsExactly(entry(FOO, BAR)); }
@Test void typedConcreteMapTypesMightBePopulated() throws Exception { when(context.getParameters()).thenReturn(parameters); when(easyRandom.doPopulateBean(String.class, context)).thenReturn(FOO, BAR); Field field = Foo.class.getDeclaredField("typedConcreteMap"); Map<String, String> randomMap = (Map<String, String>) mapPopulator.getRandomMap(field, context); assertThat(randomMap).containsExactly(entry(FOO, BAR)); }
@Test void notAddNullKeysToMap() throws NoSuchFieldException { when(context.getParameters()).thenReturn(parameters); when(easyRandom.doPopulateBean(String.class, context)).thenReturn(null); Field field = Foo.class.getDeclaredField("typedConcreteMap"); Map<String, String> randomMap = (Map<String, String>) mapPopulator.getRandomMap(field, context); assertThat(randomMap).isEmpty(); }