truth icon indicating copy to clipboard operation
truth copied to clipboard

Adding ClassSubject#isAnnotatedWith(Class)

Open benjamin-bader opened this issue 11 years ago • 6 comments
trafficstars

How does Truth feel about adding things to ClassSubject?

There are assertions that I'd like to make using reflection (e.g. assertThat(someClass).isAnnotatedWith(Singleton.class);) that aren't currently supported without custom subjects. Is there any strong feeling for or against adding at least this assertion?

I haven't thought very deeply about inheritance, and there are probably arguments to be made in that area. If there isn't any opposition, I'll fork and get to work on it.

Thanks!

benjamin-bader avatar Oct 07 '14 03:10 benjamin-bader

Added it to our API review for today...thanks!

kluever avatar Oct 07 '14 15:10 kluever

Sorry - we didn't get to it in API review this week. But at a high level, I like the concept.

On Tue, Oct 7, 2014, 08:09 Kurt Alfred Kluever [email protected] wrote:

Added it to our API review for today...thanks!

— Reply to this email directly or view it on GitHub https://github.com/google/truth/issues/139#issuecomment-58199871.

cgruber avatar Oct 07 '14 23:10 cgruber

No worries. Would it be better to defer work on this until after it is addressed in review?

benjamin-bader avatar Oct 08 '14 03:10 benjamin-bader

@benjamin-bader Yep - let's hold off until it's greenlit @ API review. Thanks!

kluever avatar Oct 08 '14 06:10 kluever

Looks like we never ended up discussing this, but I think we'd like to hear a bit more about the use-case you had in mind?

kluever avatar Dec 08 '14 22:12 kluever

Sure. The impetus for me was writing some Jersey Injectable implementations which, to be automatically registered with the runtime, must be annotated with @Provider. I think assertThat(klass).isAnnotatedWith(Provider.class) reads much more nicely than assert_().that(anno).named("has @Provider").isNotNull().

Since then, I've been playing around with generated classes and wishing to ensure that they have the expected annotations, but that's a decidedly more unusual case (for me, at least).

The important point to me is that there are cases where a missing or incorrect annotation will cause incorrect behavior at runtime, but whose absence/misuse will not be caught by the compiler - therefore requiring test coverage.

benjamin-bader avatar Dec 11 '14 17:12 benjamin-bader