Skip to content

Instantly share code, notes, and snippets.

@helospark
Created August 25, 2017 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helospark/8575a49c4857a0bbe9332c0e82d15dfe to your computer and use it in GitHub Desktop.
Save helospark/8575a49c4857a0bbe9332c0e82d15dfe to your computer and use it in GitHub Desktop.
import static org.mockito.BDDMockito.given;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class FailingTestWithCollections {
@Mock
private TestCollectionSourceProvider testCollectionSourceProvider;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testShouldNotFailButDoes() {
// Following throws: java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.util.ArrayList
given(testCollectionSourceProvider.getCollection(new ArrayList<>(), Long.class)).willReturn(new ArrayList<>());
// Can be fixed by using:
// given(testCollectionSourceProvider.getCollection(anyListOf(Long.class), eq(Long.class))).willReturn(new ArrayList<>());
}
static class TestCollectionSourceProvider {
<T extends Collection<E>, E> T getCollection(T collection, Class<E> elementType) {
return collection;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment