Skip to content

Refactor JUnit extensions to avoid using static fields.#11829

Open
slawekjaranowski wants to merge 3 commits intoapache:masterfrom
slawekjaranowski:junit-ext-static
Open

Refactor JUnit extensions to avoid using static fields.#11829
slawekjaranowski wants to merge 3 commits intoapache:masterfrom
slawekjaranowski:junit-ext-static

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.


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.
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.

Review Summary

Claude Code on behalf of Guillaume Nodet

The direction of this PR is sound — replacing static mutable state with ThreadLocal and JUnit ExtensionContext.Store is the idiomatic JUnit 5 approach for parallel test support. The namespace isolation (MAVEN_DI_EXTENSION / MOJO_EXTENSION) instead of Namespace.GLOBAL is also a good improvement.

However, there are a couple of behavioral changes mixed into this refactoring that need attention:

# Severity Issue
1 Bug @Basedir absent → basedir now resolves to a test-specific subdirectory instead of pluginBasedir
2 Medium alignToBaseDirectory(tmodel, ...)alignToBaseDirectory(model, ...) is a behavior change that should be documented or separated
3 Nit Missing blank line before Javadoc
4 Info setContext changed from protected instance to protected static — public API change

} else {
basedir = basedir.replace("${basedir}", pluginBasedir);
}
.orElse(null);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: behavioral change when @Basedir is absent.

The original code uses .orElse(pluginBasedir), so when @Basedir is not present, basedir = pluginBasedir (a real, non-empty path). Then the if (basedir.isEmpty()) check is false, so basedir stays as pluginBasedir.

With this change (.orElse(null)), when @Basedir is absent, basedir == null enters the if branch and basedir = pluginBasedir + "/target/tests/ClassName/methodName" — a completely different directory.

This contradicts the @Basedir Javadoc: "If not specified, the plugin's base directory will be used as the default." and will break any plugin test that doesn't use @Basedir but expects getBasedir() to return the plugin root.

Suggested fix — restore original semantics:

.orElse(pluginBasedir);

if (basedir.isEmpty()) {
    basedir = pluginBasedir + "/target/tests/"
            + context.getRequiredTestClass().getSimpleName() + "/"
            + context.getRequiredTestMethod().getName();
} else {
    basedir = basedir.replace("${basedir}", pluginBasedir);
}

Claude Code on behalf of Guillaume Nodet

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

looks like basedir can be only empty if system property basedir will be empty
more tests are needed for basedir annotation

tmodel = new DefaultModelPathTranslator(new DefaultPathTranslator())
.alignToBaseDirectory(tmodel, Paths.get(getBasedir()), null);
context.getStore(ExtensionContext.Namespace.GLOBAL).put(Model.class, tmodel);
.alignToBaseDirectory(model, Paths.get(getBasedir()), null);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Undocumented behavior change: tmodel to model.

The original passes tmodel (the raw parsed POM) to alignToBaseDirectory, while this PR passes model (the merged model). This is arguably an improvement — it fixes a potential NPE when tmodel is null, and aligning the merged model makes more sense. But it's a behavioral change mixed into a refactoring PR without being called out.

Consider either:

  • Documenting this in the PR description as an intentional fix, or
  • Splitting it into a separate commit

Claude Code on behalf of Guillaume Nodet

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ok revert it .... but looks like bug - to fix in separate one


/**
* Stores the extension context for use during test execution.
*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note: this changes the method signature from protected instance to protected static. This is a public API change — any downstream class overriding setContext would break at compile time. In this codebase only MojoExtension extends MavenDIExtension (and doesn't override it), but maven-testing is a public artifact used by plugin developers.

Claude Code on behalf of Guillaume Nodet

@slawekjaranowski slawekjaranowski requested a review from gnodet March 24, 2026 19:51
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.

Claude Code on behalf of Guillaume Nodet

Thanks for the quick fixes! The basedir behavior is now correct, the tmodel/model change has been reverted, the blank line is fixed, and setContext stays as a protected instance method. One minor remaining suggestion:

@Override
public void afterEach(ExtensionContext context) throws Exception {
Injector injector = context.getStore(MAVEN_DI_EXTENSION).get(Injector.class, Injector.class);
if (injector != null) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: if injector.dispose() throws, the ThreadLocal is never cleaned up. Consider wrapping in try-finally:

try {
    Injector injector = context.getStore(MAVEN_DI_EXTENSION).get(Injector.class, Injector.class);
    if (injector != null) {
        injector.dispose();
    }
} finally {
    EXTENSION_CONTEXT_THREAD_LOCAL.remove();
}

Claude Code on behalf of Guillaume Nodet

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

@slawekjaranowski slawekjaranowski requested a review from gnodet March 26, 2026 20:44
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.

2 participants