Skip to content

contains after extracting(Function...) no longer accepts values with ancestor type #3709

@scordio

Description

@scordio

The following compiles with 3.26.3:

record Container(String name, ArrayList<Object> elements) {
}

Container container = new Container("", new ArrayList<>());

assertThat(container)
  // returns AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>>
  .extracting(Container::name, Container::elements)
  .contains("", List.of());

However, it doesn't compile with 3.27.0:

assertThat(container)
  // returns AbstractListAssert<?, List<? extends Serializable>, Serializable, ObjectAssert<Serializable>>
  .extracting(Container::name, Container::elements)
  .contains("", List.of()); // Cannot resolve method 'contains(String, List<E>)'

This is a consequence of:

which changed the minimum accepted type of contains values from Object to Serializable.

Arguably, the following wouldn't compile even with 3.26.3:

assertThat(List.of(container.name(), container.elements()))
  .contains("", List.of()); // Cannot resolve method 'contains(String, List<E>)'

Therefore, I'm unsure whether the previous behavior of extracting chained with contains should be supported.

The workaround for both examples is to create an ArrayList from the expected List:

import static org.assertj.core.util.Lists.newArrayList;

assertThat(container)
  .extracting(Container::name, Container::elements)
  .contains("", newArrayList());

assertThat(List.of(container.name(), container.elements()))
  .contains("", newArrayList());

Metadata

Metadata

Assignees

Labels

status: declinedA suggestion or change that we don't feel we should currently apply

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions