Skip to content

Improve interoperability of asInstanceOf with Set instances #3322

@scordio

Description

@scordio

Right now, there is no nice way to call asInstanceOf when a Set instance is expected.

Current solutions:

Object actual = Set.of("first", "second");

assertThat(actual)
  .asInstanceOf(InstanceOfAssertFactories.type(Set.class))
  .satisfies(set -> assertThat(set).containsExactly("first", "second")); // unchecked warning

assertThat(actual)
  .asInstanceOf(InstanceOfAssertFactories.COLLECTION) // no implicit `isInstanceOf(Set.class)`
  .containsExactly("first", "second"));

assertThat(map.get("value"))
  .asInstanceOf(new InstanceOfAssertFactory<>(Set.class, Assertions::<String> assertThat)) // verbose
  .containsExactly("first", "second");

By design, we didn't introduce any InstanceOfAssertFactory for types that don't have more specific assertions, i.e., there is no AbstractSetAssert today because it wouldn't have any added value compared to AbstractCollectionAssert.

We should investigate how to improve such use cases.

Triggered by: https://github.com/spring-projects/spring-framework/blob/2784f6008e97c7283b2e57e98d18a89fe8f530ae/spring-beans/src/test/java/org/springframework/beans/factory/config/YamlProcessorTests.java#L144-L153

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions