Skip to content

Add annotation Class navigation method#3373

Open
michaelcowan wants to merge 5 commits intoassertj:mainfrom
michaelcowan:add_has_annotation_satisfying
Open

Add annotation Class navigation method#3373
michaelcowan wants to merge 5 commits intoassertj:mainfrom
michaelcowan:add_has_annotation_satisfying

Conversation

@michaelcowan
Copy link

Check List:

Add hasAnnotationSatisfying Class assertion.

This can be useful when needing to both assert that an annotation is present but also has specific parameter values
e.g., A Spring Boot engineer might want to verify specifically configured transactionals:

assertThat(MyService.class)
    .hasAnnotationSatisfying(Transactional.class, transactional ->
        assertThat(transactional)
            .extracting(Transactional::noRollbackFor)
            .isEqualTo(SendMailFailureException.class));

@scordio
Copy link
Member

scordio commented Feb 23, 2024

What about a navigation method to change the object under test?

Something like:

assertThat(MyService.class)
  .annotation(Transactional.class) // would return an AbstractObjectAssert<?, Transactional>
  .extracting(Transactional::noRollbackFor)
  .isEqualTo(SendMailFailureException.class));

@michaelcowan
Copy link
Author

@scordio That was actually my first instinct too, but I changed my mind when I considered:

  1. The existing pattern of isInstanceOf and isInstanceOfSatisfying
    • Which also supports chaining more assertions
  2. We already have hasAnnotation

This made me think that what we are missing is a hasAnnotationSatisfying assertion.

Happy to change this, I just want to be sure that what we want is to have both:

assertThat(MyService.class)
  .hasAnnotation(Transactional.class);

-and-

assertThat(MyService.class)
  .annotation(Transactional.class)
  // ...

@scordio
Copy link
Member

scordio commented Feb 23, 2024

We discussed it internally and we would generally favor navigation methods for new APIs.

hasAnnotation(...) can still have a purpose if no navigation is needed, similar to hasCause() and cause() for Throwable assertions.

As a side note, asInstanceOf also acts as a navigation method alongside isInstanceOf 🙂

@michaelcowan
Copy link
Author

@scordio Understood - I've pushed those changes to the branch.

@michaelcowan michaelcowan changed the title Add hasAnnotationSatisfying Class assertion Add annotation Class navigation method Feb 25, 2024
@michaelcowan
Copy link
Author

@scordio Do you want me to open a new PR and close this one?

@michaelcowan michaelcowan force-pushed the add_has_annotation_satisfying branch from ff425d7 to 481920b Compare March 12, 2024 18:20
@scordio scordio force-pushed the 3.x branch 2 times, most recently from 301ca01 to c730d18 Compare June 1, 2024 16:04
@michaelcowan
Copy link
Author

@scordio If there anything from me blocking this being merged?

@scordio
Copy link
Member

scordio commented Mar 9, 2025

@michaelcowan no, not at all. We are focusing on releasing the first milestone of version 4 and haven't had the chance to finalize your PR.

I'll schedule these changes for M2, and apologies for the extremely slow feedback!

@scordio scordio added this to the 4.0.0-M2 milestone Mar 9, 2025
@scordio scordio changed the base branch from 3.x to main March 9, 2025 22:07
@scordio scordio closed this Apr 24, 2025
@scordio scordio reopened this Apr 24, 2025
Comment on lines +57 to +62
@Nested
class With_Repeatable_Annotation {

// TODO

}
Copy link
Member

@scordio scordio Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We lack coverage for repeatable annotations. I'll look into that first with hasAnnotation and hasAnnotations, and align this branch later on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More coverage for hasAnnotation done in #3823. Another set of changes will follow for hasAnnotations, and then the same will be applied to this branch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for hasAnnotations added with 5c4978f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants