CollectionOfMocksInitialiser { @SuppressWarnings("rawtypes") public void initialise(Object object) { Set<Field> fields = annotatedFieldRetriever.getAnnotatedFields(object.getClass(), CollectionOfMocks.class); for (Field field : fields) { Collection collection = createCollectionForField(field); new FieldValueMutator(object, field).mutateTo(collection); } } CollectionOfMocksInitialiser(AnnotatedFieldRetriever annotatedFieldRetriever,
			GenericCollectionTypeResolver genericCollectionTypeResolver, CollectionFactory collectionFactory,
			MockStrategy mockStrategy); @SuppressWarnings("rawtypes") void initialise(Object object);  }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void shouldInitialise() { ClassWithAnnnotations object = new ClassWithAnnnotations(); Field field = getField(object.getClass(), "collection"); Set<Field> fields = Collections.singleton(field); given(annotatedFieldRetriever.getAnnotatedFields(object.getClass(), CollectionOfMocks.class)) .willReturn(fields); Class collectionType = EventListener.class; given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn(collectionType); EventListener mockEventListener = mock(EventListener.class); given(mockStrategy.createMock(EventListener.class)).willReturn(mockEventListener); Collection collection = mock(Collection.class); given( collectionFactory.createCollection(eq(Collection.class), eq(new OrderedSet(Collections.singleton(mockEventListener))))).willReturn(collection); initialiser.initialise(object); assertSame(collection, object.collection); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void shouldInitialiseGivenAnnotationWithZeroMocks() { ClassWithAnnnotations object = new ClassWithAnnnotations(); Field field = getField(object.getClass(), "collectionWithZeroMocks"); Set<Field> fields = Collections.singleton(field); given(annotatedFieldRetriever.getAnnotatedFields(object.getClass(), CollectionOfMocks.class)) .willReturn(fields); Class collectionType = EventListener.class; given(genericCollectionTypeResolver.getCollectionFieldType(field)).willReturn(collectionType); Collection collection = mock(Collection.class); given(collectionFactory.createCollection(Collection.class, new OrderedSet())).willReturn(collection); initialiser.initialise(object); assertSame(collection, object.collectionWithZeroMocks); }