junit5 icon indicating copy to clipboard operation
junit5 copied to clipboard

Allow `ArgumentsProvider`s to register additional extensions

Open fmeum opened this issue 2 years ago • 7 comments

I have a use case in which I would like to intercept only certain invocations of a @ParameterizedTest, namely those where the arguments come from a particular ArgumentsProvider. Since the information about which ArgumentsProvider provided the current set of arguments isn't available in e.g. InvocationInterceptor#interceptTestTemplateMethod, this doesn't seem to be possible today.

TestTemplateInvocationContext#getAdditionalExtensions would allow doing that, but it can't be populated from an ArgumentsProvider. I thus propose exposing this functionality via a new getAdditionalExtensions() method on ArgumentsProvider.

Deliverables

  • [ ] Add a new default method List<Extension> getAdditionalExtensions() to ArgumentsProvider. In ParameterizedTestExtension, forward the additional extensions to the ParameterizedTestInvocationContext.

fmeum avatar May 05 '23 08:05 fmeum

@fmeum I did a quick spike of making the ArgumentsProvider accessible via ExtensionContext. Would that work for your use case?

marcphilipp avatar May 06 '23 13:05 marcphilipp

@marcphilipp That's a neat solution for my use case with little new API surface. I like it, thanks for the quick response!

fmeum avatar May 06 '23 16:05 fmeum

I noticed one limitation that makes this solution slightly less useful than I first thought: A single annotation cannot be meta-annotated with multiple @ArgumentsSources since @ArgumentsSources does not list ANNOTATION_TYPE in @Target. Is this intentional?

As a result, we can't just have our custom @ParameterizedTest annotation register multiple ArgumentsProviders that we can then distinguish using the instance reference in the store. The proposed change would still help us distinguish our own arguments from those provided by user-registered @ArgumentsSources though.

Edit: I confirmed that this can be worked around by making the second @ArgumentsSource an annotation on a new helper annotation that can then be applied to the same annotation as @ArgumentsSource.

fmeum avatar May 08 '23 21:05 fmeum

@marcphilipp Since I don't know the usual processes for contributions yet: Would it help if I turned your spike into a PR with tests and docs or is this something you would like to do yourself?

fmeum avatar May 10 '23 13:05 fmeum

@fmeum Thanks for asking! I'd like to discuss this with the team first to avoid making you go through that unnecessarily.

marcphilipp avatar May 13 '23 09:05 marcphilipp

Team decision: Wait for additional interest from the community since we don't perceive this as a common use case.

marcphilipp avatar Jun 30 '23 10:06 marcphilipp

For what it's worth, I would also find this to be a useful addition and have been wanting to do something similar to what fmeum is suggesting.

rendenyoder avatar May 29 '24 18:05 rendenyoder