### Question:
CollectionPopulator { @SuppressWarnings({"unchecked", "rawtypes"}) Collection<?> getRandomCollection(final Field field, final RandomizationContext context) { int randomSize = getRandomCollectionSize(context.getParameters()); Class<?> fieldType = field.getType(); Type fieldGenericType = field.getGenericType(); Collection collection; if (isInterface(fieldType)) { collection = getEmptyImplementationForCollectionInterface(fieldType); } else { collection = createEmptyCollectionForType(fieldType, randomSize); } if (isParameterizedType(fieldGenericType)) { ParameterizedType parameterizedType = (ParameterizedType) fieldGenericType; Type type = parameterizedType.getActualTypeArguments()[0]; if (isPopulatable(type)) { for (int i = 0; i < randomSize; i++) { Object item = easyRandom.doPopulateBean((Class<?>) type, context); collection.add(item); } } } return collection; } CollectionPopulator(final EasyRandom easyRandom);   }### Answer:
@Test void rawInterfaceCollectionTypesMustBeReturnedEmpty() throws Exception { when(context.getParameters()).thenReturn(parameters); Field field = Foo.class.getDeclaredField("rawInterfaceList"); Collection<?> collection = collectionPopulator.getRandomCollection(field, context); assertThat(collection).isEmpty(); }
@Test void rawConcreteCollectionTypesMustBeReturnedEmpty() throws Exception { when(context.getParameters()).thenReturn(parameters); Field field = Foo.class.getDeclaredField("rawConcreteList"); Collection<?> collection = collectionPopulator.getRandomCollection(field, context); assertThat(collection).isEmpty(); }
@Test void typedInterfaceCollectionTypesMightBePopulated() throws Exception { when(context.getParameters()).thenReturn(parameters); when(easyRandom.doPopulateBean(String.class, context)).thenReturn(STRING); Field field = Foo.class.getDeclaredField("typedInterfaceList"); @SuppressWarnings("unchecked") Collection<String> collection = (Collection<String>) collectionPopulator.getRandomCollection(field, context); assertThat(collection).containsExactly(STRING, STRING); }
@Test void typedConcreteCollectionTypesMightBePopulated() throws Exception { when(context.getParameters()).thenReturn(parameters); when(easyRandom.doPopulateBean(String.class, context)).thenReturn(STRING); Field field = Foo.class.getDeclaredField("typedConcreteList"); @SuppressWarnings("unchecked") Collection<String> collection = (Collection<String>) collectionPopulator.getRandomCollection(field, context); assertThat(collection).containsExactly(STRING, STRING); }