google / google/truth

Matching on exact class type, not a subtype

Open
#1,230 3 comments 0 reactions 0 assignees View on GitHub
P3 type=enhancement
Dominant language
Java
Stars
2.8k
Forks
275
Avg merge
7m
Merged PRs (30d)
4

Description

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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.