Created
August 25, 2017 10:32
-
-
Save helospark/8575a49c4857a0bbe9332c0e82d15dfe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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