Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotation Class navigation method #3373

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from

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?

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.

None yet

2 participants