@Test
public void testToMultimapWithCollectionFactory() {

    Observable<String> source = Observable.from("cc", "dd", "eee", "eee");
    Func1<Integer, Collection<String>> collectionFactory = new Func1<Integer, Collection<String>>() {
        @Override
        public Collection<String> call(Integer t1) {
            if (t1 == 2) {
                return new ArrayList<String>();
            } else {
                return new HashSet<String>();
            }
        }
    };
    Observable<Map<Integer, Collection<String>>> mapped = Observable.create(
            OperationToMultimap.toMultimap(
                    source, lengthFunc, Functions.<String> identity(),
                    new DefaultToMultimapFactory<Integer, String>(), collectionFactory));
    Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
    expected.put(2, Arrays.asList("cc", "dd"));
    expected.put(3, new HashSet<String>(Arrays.asList("eee")));
    mapped.subscribe(objectObserver);
    verify(objectObserver, never()).onError(any(Throwable.class));
    verify(objectObserver, times(1)).onNext(expected);
    verify(objectObserver, times(1)).onCompleted();
}