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

Support declarative extension registration on fields and parameters #864

Closed
nipafx opened this issue May 25, 2017 · 20 comments
Closed

Support declarative extension registration on fields and parameters #864

nipafx opened this issue May 25, 2017 · 20 comments

Comments

@nipafx
Copy link
Contributor

nipafx commented May 25, 2017

Overview

Original title: "Support ParameterResolver registration via @ResolveWith on parameter"

While experimenting with parameter resolvers I realized that they can not be applied like argument converters can, i.e. by annotating the parameter directly. This complicates their application and implementation if they are "annotation based".

An example is the @Mock proof of concept. The resolver has to be applied to the test method or class and because of that has to check the individual parameter for the presence of the @Mock annotation. This indirection causes unnecessary complexity for implementers and users of the extension.

Original Proposal

I propose an annotation similar to @ConvertWith, e.g. @ResolveWith, that takes precedence over all registered parameter resolvers. It does not need a supports method because it is assumed to support the parameters it is applied to. If it turns out during resolve that that's not the case, the test should be aborted by an exception.

With this mechanism, @Mock (and I am planning to write something like @Random) would get simpler to use because they only need to be applied in one place.

More broadly, I think parameter resolvers and argument converters are conceptually very similar and the API would hence improve considerably if both would operate similarly as well. In that regard I already opened #853, which proposes an extension point to register converters at the method and class level. Together with this issue, the two aim to align both mechanisms.

If there is interest in this, I would give it a shot, preferably together with #853.

Related Issues

@sormuras
Copy link
Member

sormuras commented May 25, 2017

Like?

class SudokuTests {
  @RepeatedTest(10)
  void testSomeGeneratedSudokus(@Random int seed) {
    assertTrue(Sudoku.generate(seed).isSolvable());
  }
}

Edit: now with simple class body w/o any parameter resolver on the class level.

@nipafx
Copy link
Contributor Author

nipafx commented May 25, 2017

Yes. But without registering any parameter resolver on the class level and without the chance of @Random conflicting with them.

@sormuras sormuras added this to the 5.0 M6 milestone May 25, 2017
@marcphilipp marcphilipp modified the milestones: 5.0 M6, 5.1 Backlog Jul 18, 2017
@sbrannen sbrannen changed the title Allow parameter resolution with @ResolveWith Support ParameterResolver registration via @ResolveWith on parameter Jul 27, 2018
@sbrannen
Copy link
Member

sbrannen commented Jul 27, 2018

Related discussion on Twitter about being able to register extensions on parameters:

https://twitter.com/olivergierke/status/1022802925603758080

@sbrannen sbrannen modified the milestones: 5.x Backlog, 5.3 Backlog Aug 2, 2018
@sbrannen sbrannen changed the title Support ParameterResolver registration via @ResolveWith on parameter Support extension registration on parameters Aug 22, 2018
@sbrannen sbrannen changed the title Support extension registration on parameters Support declarative extension registration on parameters Aug 22, 2018
@sbrannen
Copy link
Member

Renamed issue to align with the title of #497 and features discussed in the aforementioned Twitter thread.

@sbrannen sbrannen self-assigned this Jan 25, 2019
@sbrannen sbrannen changed the title Support declarative extension registration on parameters Support declarative extension registration on fields and parameters Jan 25, 2019
@sbrannen
Copy link
Member

Updated scope to include registration via fields and parameters.

@sbrannen
Copy link
Member

Current work on this issue can be viewed in the following branch:

https://github.com/junit-team/junit5/compare/issues/864-declarative-extension-registration-params-fields

@sbrannen
Copy link
Member

in progress

@sbrannen sbrannen removed this from the 5.5 Backlog milestone Jan 26, 2019
sbrannen added a commit that referenced this issue Aug 17, 2021
…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
sbrannen added a commit that referenced this issue Aug 17, 2021
This commit introduces extensions registered explicitly via
@RegisterExtension to verify the respective ordering with extensions
registered implicitly via @ExtendWith on fields.

Issue: #864
sbrannen added a commit that referenced this issue Aug 17, 2021
Issue: #864
sbrannen added a commit that referenced this issue Aug 17, 2021
sbrannen added a commit that referenced this issue Aug 17, 2021
Prior to this commit, an exception was thrown if a @RegisterExtension
field was declared private.

This commit removes this restriction.

Closes #2688
See #864, #2680
sbrannen added a commit that referenced this issue Aug 17, 2021
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
sbrannen added a commit that referenced this issue Aug 17, 2021
…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
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
This commit introduces extensions registered explicitly via
@RegisterExtension to verify the respective ordering with extensions
registered implicitly via @ExtendWith on fields.

Issue: junit-team#864
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
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
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
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
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
…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
runningcode pushed a commit to runningcode/junit5 that referenced this issue Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment