Skip to content

Refactor JUnit extensions to avoid using static fields.#12202

Merged
slawekjaranowski merged 1 commit into
apache:maven-4.0.xfrom
slawekjaranowski:cp-11829
Jun 4, 2026
Merged

Refactor JUnit extensions to avoid using static fields.#12202
slawekjaranowski merged 1 commit into
apache:maven-4.0.xfrom
slawekjaranowski:cp-11829

Conversation

@slawekjaranowski
Copy link
Copy Markdown
Member

Static filed in Junit extension can impact on tests executed in parallel. For static methods preserver current test context in local thread.

(cherry picked from commit 52c4d43)

Following this checklist to help us incorporate your
contribution quickly and easily:

  • Your pull request should address just one issue, without pulling in other changes.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body.
    Note that commits might be squashed by a maintainer on merge.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
    This may not always be possible but is a best-practice.
  • Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • You have run the Core IT successfully.

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

Static filed in Junit extension can impact on tests executed in parallel.
For static methods preserver current test context in local thread.

(cherry picked from commit 52c4d43)
Copy link
Copy Markdown
Contributor

@gnodet gnodet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean cherry-pick of #11829 from master to maven-4.0.x. The refactoring correctly replaces static mutable fields (context, injector, basedir) with ThreadLocal + ExtensionContext.Store, making the JUnit extensions safe for parallel test execution.

Verified:

  • ThreadLocal cleanup in afterEach() is in a finally block — no leak risk
  • Store lifecycle is managed by JUnit automatically
  • Ordering in MojoExtension.beforeEach() is correct — setContext() before any getContext() consumers
  • Namespace isolation (MAVEN_DI_EXTENSION, MOJO_EXTENSION) is an improvement over GLOBAL
  • Null check removal for basedir is safe — MavenDIExtension.getBasedir() always returns non-null
  • Test updates in SimpleDITest match the new setupContainer(ExtensionContext) signature

CI green across all 22 checks (ubuntu/macos/windows, JDK 17/21/25).

Fully automatic review from Claude Code. This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

@slawekjaranowski slawekjaranowski merged commit c8abb9c into apache:maven-4.0.x Jun 4, 2026
22 checks passed
@slawekjaranowski slawekjaranowski deleted the cp-11829 branch June 4, 2026 13:53
@github-actions github-actions Bot added this to the 4.0.0-rc-6 milestone Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants