Refactor JUnit extensions to avoid using static fields.#11829
Refactor JUnit extensions to avoid using static fields.#11829slawekjaranowski wants to merge 3 commits intoapache:masterfrom
Conversation
Static filed in Junit extension can impact on tests executed in parallel. For static methods preserver current test context in local thread.
a6673c0 to
7cbbaaa
Compare
gnodet
left a comment
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
ok revert it .... but looks like bug - to fix in separate one
impl/maven-testing/src/main/java/org/apache/maven/api/di/testing/MavenDIExtension.java
Show resolved
Hide resolved
|
|
||
| /** | ||
| * Stores the extension context for use during test execution. | ||
| * |
There was a problem hiding this comment.
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
gnodet
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
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:
Note that commits might be squashed by a maintainer on merge.
This may not always be possible but is a best-practice.
mvn verifyto make sure basic checks pass.A more thorough check will be performed on your pull request automatically.
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.