Skip to content

Matching on exact class type, not a subtype #1230

@JakeWharton

Description

@JakeWharton

I'm testing some library APIs which throw various exceptions that are documented as part of its API contract. However, some of the exceptions are subtypes of each other. I want to assert that an exception subject is an instance of a precise type and not some subtype.

Now I can do assertThat(e.getClass()).isEqualTo(SomeException.class);, sure, but that does not communicate my intent well. An innocent passer-by might see that, assume I don't know the Truth APIs well, and helpfully rewrite that into a traditional isInstanceOf not realizing that they're actually relaxing the constraint.

AssertJ has isExactlyInstanceOf which is pretty spot on here, and I think communicates intent well. I tried quickly thinking of other names but didn't find any that compelling (hasClassEqualTo 🤢).

Thoughts? Find anyone doing assertThat(x.getClass()) in google3?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3not scheduledtype=enhancementMake an existing feature better

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions