Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic assertThat(ArrayList, hasItems(InstanceOfSomeInterface)) not working

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)?

like image 635
dermoritz Avatar asked Oct 11 '22 00:10

dermoritz


1 Answers

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.

like image 96
John B Avatar answered Oct 18 '22 08:10

John B