-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
TestInstancePostProcessor
cannot be registered via @ExtendWith
on a non-static field
#3437
Comments
Should I be able to inject a Example: class BasicTests {
@ExtendWith(ExtensionInjector.class)
ExtensionInjector injector;
@RegisterExtension
ParameterResolver extension;
// some tests...
} Also: can I work on this issue? Other than this one question, I think I can handle it. |
Hi @Michael1993,
Ideally, yes, I believe so. At least, generally speaking a I believe I demonstrated that once with Spring. Let me see if I can find it... |
|
Thanks for volunteering! However, this issue is assigned to me and not labeled as In addition, it may turn out to be more challenging than you expect. In light of that, I would like to keep this assigned to me. FWIW, I took a glance at your spike, and I'd like to point out that we have no plans to add specific handling for a particular extension API (such as |
Sorry, I don't know how I missed that. I will take a look at the up-for-grabs issues instead, then. 🙂 |
This commit is truly just an "experiment". The actual "fix" might take a completely different approach. Various tests in ExtensionRegistrationViaParametersAndFieldsTests fail with the changes in this commit. Issue: junit-team#3437
I pushed an "experiment" to the following feature branch. Note, however, that this is truly a work in progress. A few existing tests fail, and we may need to take a completely different approach. Thus, I've only pushed this to allow others (for example, @marcphilipp) to take a look at this for potential inspiration. main...sbrannen:junit5:issues/3437-ExtendWith-on-fields-early-registration |
This PR changes extensions declared via @ExtendWith on non-static fields to be registered before the test instance is instantiated. Since these declarative extensions are documented to be sorted along with programmatic extensions registered via @RegisterExtension the latter are also registered earlier. Since, however, non-static fields are not initialized until the test class is instantiated, this PR introduces the internal concept of "uninitialized extensions" that are registered early and initialized whenever an instance of the test class is created. Fixes #3437. Resolves #3463. Co-authored-by: Sam Brannen <104798+sbrannen@users.noreply.github.com>
Overview
As discovered by @graydavid in #3433, there is an issue regarding extension registration via
@ExtendWith
on instance fields.The following is a simplification of the
RandomNumberDemo
from the User Guide which demonstrates the problem.The above test fails; however, if you annotate the test class with
@ExtendWith(RandomNumberExtension.class)
the test then passes.The reason is that
ClassBasedTestDescriptor#instantiateAndPostProcessTestInstance()
invokesExtensionUtils.registerExtensionsFromFields()
too late.I even added the following comment when I introduced that support.
In retrospect, that is not the "best time to do that" when a field is annotated (or meta-annotated in the case of
@Random
) with an@ExtendWith
annotation that registers an extension that implementsTestInstancePostProcessor
.Related Issues
RandomNumberExtension
in the User Guide #3433RandomNumberExtension
in the User Guide #3463Deliverables
ExtensionUtils.registerExtensionsFromFields()
so that aTestInstancePostProcessor
can be registered via@ExtendWith
on a non-static field.The text was updated successfully, but these errors were encountered: