@Test
public void testToMultimapWithThrowingCollectionFactory() {

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