Fix ExtensionContext on instance creation #4032
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Resolve #1568.
Resolve #2970.
Resolve #3445.
This PR moves the instance creation into the execution context of the test method. This affects the following callbacks:
TestInstancePreConstructCallback
TestInstanceFactory
ParameterResolver
(when resolving parameters for the constructor)TestInstancePostProcessor
Assuming the test is using
TestInstance.Lifecycle.PER_METHOD
(the default), these callbacks will now receive theExtensionContext
for the currently executed test method. This has the following effects:ExtensionContext.getTestMethod()
ExtensionContext.getTestClass()
andTestInstanceFactoryContext.getTestClass()
are no-longer interchangeableExtensionContext.getTestClass()
now returns the (nested) class of the currently executed testCloseableResource
which are added to the store will now be closed at the end of the test executionIf the test is using
TestInstance.Lifecycle.PER_CLASS
, the behavior is unaltered.Not sure if the change is considered API breaking. The previous behavior was rather unintuitive and not explicitly documented. However, the example at
TestInfoDemo
is no-longer valid and has been changed as part of the PR. I also think there is a good chance that there are a few extensions in the wild which rely on the previous behavior, since the behavior was in place for multiple years. I would therefore like to know how I should proceed with the PR.I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@API
annotations