5

i want to use Hamcrest’s hasItems with an "actual" collection that is an ArrayList<? extends SomeInterface> on

assertThat(ArrayList<? extends SomeInterface>, hasItems(InstanceOfSomeInterface))

the compiler says:

The method assertThat(T, Matcher<T>) in the type Assert is not applicable for the arguments (ArrayList<capture#9-of ? extends MyInterface>, Matcher<Iterable<MyInterface>>)

what is going wrong? What can i do about that (i really want to use Hamcrest here)?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
dermoritz
  • 12,519
  • 25
  • 97
  • 185
  • Can you post the code showing the types of the collection? – John B Sep 14 '11 at 10:33
  • my collection is of type extends IsTreeItem>: `private ArrayList extends IsTreeItem> filteredAdminUnits;` and this i want to use like this `assertThat(filteredAdminUnits, hasItems(anTreeItem))` – dermoritz Sep 14 '11 at 11:53

1 Answers1

4
ArrayList<SomeInterface> newList = new ArrayList<SomeInterface>();
newList.addAll(origList);
assertThat(newList, hasItems(InstanceOfSomeInterface));

It is unfortunate that Assert.assertThat was not coded using ? super or ? extends to allow for what you describe.

John B
  • 32,493
  • 6
  • 77
  • 98