From 58559534e66056c0634a9f0b97bdb901d6f2deda Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 15 Jul 2026 14:15:29 +0200 Subject: [PATCH] AddMockitoJavaAgentToMavenSurefirePlugin: add surefire config in reactor modules When a module's parent pom is part of the same reactor and manages the surefire and dependency plugins in pluginManagement (without declaring them in build/plugins), the recipe added an empty argLine property and the maven-dependency-plugin, but skipped adding the surefire plugin with the Java agent argLine. AddPlugin with a null filePattern refuses to run on modules whose parent is a project pom, so the surefire configuration was silently dropped while the dependency-plugin (which passes a **/pom.xml filePattern) was added, leaving a pointless partial change. Pass the same **/pom.xml filePattern for the surefire AddPlugin so the configuration is added consistently, producing a complete, working setup. Fixes #1168 --- ...MockitoJavaAgentToMavenSurefirePlugin.java | 2 +- ...itoJavaAgentToMavenSurefirePluginTest.java | 127 ++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java b/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java index b7925d2544..9b96d5da4e 100644 --- a/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java +++ b/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java @@ -136,7 +136,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { if (FindPlugin.find(document, "org.apache.maven.plugins", "maven-surefire-plugin").isEmpty()) { doAfterVisit(new AddPlugin("org.apache.maven.plugins", "maven-surefire-plugin", null, String.format(CONFIGURATION_TAG_TEMPLATE, "@{argLine} " + getArgLineJavaAgentArgument()), null, - null, null).getVisitor()); + null, "**/pom.xml").getVisitor()); return document; } return super.visitDocument(document, ctx); diff --git a/src/test/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePluginTest.java b/src/test/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePluginTest.java index cf1275308f..3158042b3b 100644 --- a/src/test/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePluginTest.java +++ b/src/test/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePluginTest.java @@ -1614,4 +1614,131 @@ void augmentsSurefirePluginDeclaredInPluginManagement() { ) ); } + + @Test + void addsSurefireAgentToModuleWhenParentReactorPomManagesPluginsWithoutDeclaringThem() { + rewriteRun( + mavenProject("test-project", + pomXml( + """ + + 4.0.0 + org.sample + test + 1.0 + pom + + + test-module1 + + + + + + org.mockito + mockito-core + 5.14.0 + test + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.11.0 + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.6 + + + + + + """, + spec -> spec.path("pom.xml") + ), + pomXml( + """ + + 4.0.0 + org.sample + test-module1 + 1.0 + + + org.sample + test + 1.0 + ../pom.xml + + + + + org.mockito + mockito-core + test + + + + """, + """ + + 4.0.0 + org.sample + test-module1 + 1.0 + + + org.sample + test + 1.0 + ../pom.xml + + + + + + + + org.mockito + mockito-core + test + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + properties + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + @{argLine} -javaagent:${org.mockito:mockito-core:jar} + + + + + + """, + spec -> spec.path("test-module1/pom.xml") + ) + ) + ); + } }