Search code examples
genericsjava-8assertj

Generics issue with AssertJ Condition for a typed List


I ran into this issue when exploring uses of the Condition class in AssertJ 3.5.2 on Java 8. I can create a Condition instance for a generically-typed List, but I get error messages in Eclipse when I try to use it :

Condition<List<MyBean>> listCond =
        new Condition<>(list -> true, "test"); // OK

this.assertThat(myList).has(listCond); // DOES NOT COMPILE

The error message I get is :

The method has(Condition<? super List<? extends MyBean>>) in the type 
AbstractListAssert<capture#8-of ?,List<? extends MyBean>,MyBean,ObjectAssert<MyBean>> is not 
applicable for the arguments (Condition<List<MyBean>>)

Is there a solution for this or another approach in AssertJ to do a holistic check of a list (not just item by item but a check based on sequence or aggregation)?


Solution

  • I believe that declaring your condition like this should solve the compile error :

    Condition<? super List<? extends MyBean>> listCond = new Condition<>(list -> true, "test");