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

Fix ExtensionContext on instance creation #4032

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JojOatXGME
Copy link

@JojOatXGME JojOatXGME commented Sep 29, 2024

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 the ExtensionContext for the currently executed test method. This has the following effects:

  • The callbacks can now access ExtensionContext.getTestMethod()
  • ExtensionContext.getTestClass() and TestInstanceFactoryContext.getTestClass() are no-longer interchangeable
  • ExtensionContext.getTestClass() now returns the (nested) class of the currently executed test
  • Instances of CloseableResource which are added to the store will now be closed at the end of the test execution

If 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant