-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Support declarative extension registration on fields and parameters #2680
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
Support declarative extension registration on fields and parameters #2680
Conversation
From one perspective, this is inconsistent treatment of fields; however, I wonder if we haven't been too strict with regard to the current non-private policy for If JUnit or a third party provides an extension that is implicitly registered by a custom composed annotation (which is meta-annotated with As long as the extension can work with a In other words, why should JUnit not allow the user to decide if a field is I recommend that we allow |
Although the original intention of #864 was to allow extension authors to provide custom composed annotations that are meta-annotated with In other words, I don't believe we should force users or library implementers to create custom composed annotations when all they need is |
The above describes the status quo in this PR, but we can certainly change the semantics. My rationale for implementing the feature with these semantics was based on the fact that extensions registered via So, the status quo in this PR is consistent in that regard. However, we have an opportunity (for the first time) to make it possible for extensions registered via Achieving that is straight forward: I'd just need to collapse the two iterations in After having spent more time with this issue, I am in favor of allowing |
Aside from the discussion about |
Codecov Report
@@ Coverage Diff @@
## main #2680 +/- ##
==========================================
+ Coverage 91.46% 91.49% +0.03%
==========================================
Files 427 427
Lines 12067 12100 +33
Branches 941 946 +5
==========================================
+ Hits 11037 11071 +34
Misses 790 790
+ Partials 240 239 -1
Continue to review full report at Codecov.
|
Related issues: |
Team Decision:
|
Prior to this commit, @ExtendWith could only be declared on types (i.e., classes, interfaces, annotations) and methods. @ExtendWith can now be declared directly on fields and parameters as well. See #864, #2680
Prior to this commit, @ExtendWith could only be declared on types (i.e., classes, interfaces, annotations) and methods. @ExtendWith can now be declared directly on fields and parameters as well. See #864, #2680
98d0ca9
to
935ce6f
Compare
…each other Prior to this commit, @ExtendWith fields and @RegisterExtension fields were sorted using @order, but extensions registered via @ExtendWith fields were always registered first (before extensions registered via @RegisterExtension fields). This commit ensures that @RegisterExtension fields and @ExtendWith fields are sorted via @order relative to each other. See #864, #2680
…each other Prior to this commit, @ExtendWith fields and @RegisterExtension fields were sorted using @order, but extensions registered via @ExtendWith fields were always registered first (before extensions registered via @RegisterExtension fields). This commit ensures that @RegisterExtension fields and @ExtendWith fields are sorted via @order relative to each other. See #864, #2680
d78650b
to
d89b9da
Compare
…each other Prior to this commit, @ExtendWith fields and @RegisterExtension fields were sorted using @order, but extensions registered via @ExtendWith fields were always registered first (before extensions registered via @RegisterExtension fields). This commit ensures that @RegisterExtension fields and @ExtendWith fields are sorted via @order relative to each other. See #864, #2680
5a97a3d
to
1c42810
Compare
Prior to this commit, @ExtendWith and @RegisterExtension could be declared on a field to register duplicate extensions of the same time, but that scenario likely does not make sense. This commit ensures that @RegisterExtension and @ExtendWith cannot be used to register an extension of the same type for a given field, by throwing a PreconditionViolationException if the user attempts to do so. See #2680
4a9d1fd
to
afe5fe2
Compare
This commit introduces extensions registered explicitly via @RegisterExtension to verify the respective ordering with extensions registered implicitly via @ExtendWith on fields. Issue: #864
Prior to this commit, @ExtendWith could only be declared on types (i.e., classes, interfaces, annotations) and methods. @ExtendWith can now be declared directly on fields and parameters as well. See #864, #2680
…each other Prior to this commit, @ExtendWith fields and @RegisterExtension fields were sorted using @order, but extensions registered via @ExtendWith fields were always registered first (before extensions registered via @RegisterExtension fields). This commit ensures that @RegisterExtension fields and @ExtendWith fields are sorted via @order relative to each other. See #864, #2680
Prior to this commit, @ExtendWith and @RegisterExtension could be declared on a field to register duplicate extensions of the same time, but that scenario likely does not make sense. This commit ensures that @RegisterExtension and @ExtendWith cannot be used to register an extension of the same type for a given field, by throwing a PreconditionViolationException if the user attempts to do so. See #2680
a740853
to
77cafeb
Compare
Prior to this commit, @ExtendWith could only be declared on types (i.e., classes, interfaces, annotations) and methods. @ExtendWith can now be declared directly on fields and parameters as well. See #864, #2680
…each other Prior to this commit, @ExtendWith fields and @RegisterExtension fields were sorted using @order, but extensions registered via @ExtendWith fields were always registered first (before extensions registered via @RegisterExtension fields). This commit ensures that @RegisterExtension fields and @ExtendWith fields are sorted via @order relative to each other. See #864, #2680
Prior to this commit, @ExtendWith and @RegisterExtension could be declared on a field to register duplicate extensions of the same time, but that scenario likely does not make sense. This commit ensures that @RegisterExtension and @ExtendWith cannot be used to register an extension of the same type for a given field, by throwing a PreconditionViolationException if the user attempts to do so. See #2680
Prior to this commit, @ExtendWith could only be used to register extensions declaratively on test interfaces, test classes, and test methods. However, there are certain use cases where it is more convenient for the user if extensions can be registered declaratively on fields and parameters. This commit introduces support for registering extensions declaratively via @ExtendWith on the following elements. - static fields - non-static fields - parameters in test class constructors, test methods, and lifecycle methods (i.e., @BeforeAll, @afterall, @beforeeach, and @AfterEach methods) Fields annotated or meta-annotated with @ExtendWith can have any visibility (including private) and can be sorted relative to @RegisterExtension fields via the @order annotation. See the RandomNumberExtension example in the User Guide. Closes #864, #2680
Closed via commit 8b5387c |
Prior to this commit, an exception was thrown if a @RegisterExtension field was declared private. This commit removes this restriction. Closes junit-team#2688 See junit-team#864, junit-team#2680
Prior to this commit, @ExtendWith could only be declared on types (i.e., classes, interfaces, annotations) and methods. @ExtendWith can now be declared directly on fields and parameters as well. See junit-team#864, junit-team#2680
…each other Prior to this commit, @ExtendWith fields and @RegisterExtension fields were sorted using @order, but extensions registered via @ExtendWith fields were always registered first (before extensions registered via @RegisterExtension fields). This commit ensures that @RegisterExtension fields and @ExtendWith fields are sorted via @order relative to each other. See junit-team#864, junit-team#2680
Prior to this commit, @ExtendWith and @RegisterExtension could be declared on a field to register duplicate extensions of the same time, but that scenario likely does not make sense. This commit ensures that @RegisterExtension and @ExtendWith cannot be used to register an extension of the same type for a given field, by throwing a PreconditionViolationException if the user attempts to do so. See junit-team#2680
Overview
This is a PR for #864.
The implementation is essentially feature complete and tested (following the 80/20 rule); however, there are some points that should be discussed before merging this PR into the
main
branch.Topics for Discussion
When discussing any of these topics, I recommend that you review the current behavior by analyzing the tests in
ExtensionRegistrationViaParametersAndFieldsTests
before spending time studying the actual implementation details.registrationOrder()
test method. Implementation details can be found inClassBasedTestDescriptor
andTestMethodTestDescriptor
.@Order
Support:ExtensionUtils.registerExtensionsFromFields()
already supported@Order
for extensions registered via@RegisterExtension
, and@Order
is now also applied to extensions registered via fields using@ExtendWith
. But...@ExtendWith
extensions are registered before@RegisterExtension
extensions.@ExtendWith
target:@ExtendWith
can currently be declared on aTYPE
orMETHOD
. It cannot be declared directly on aPARAMETER
orFIELD
.@RegisterExtension
fields are not allowed to beprivate
, but in this PR fields meta-annotated with@ExtendWith
are allowed to beprivate
.I will share my thoughts on these topics as comments and invite feedback.
Definition of Done
@API
annotations