Skip to content

Only add the Mockito surefire agent configuration when asked, and keep it minimal - #1184

Merged
timtebeek merged 7 commits into
mainfrom
mockito-javaagent-less-invasive-and-optin
Aug 4, 2026
Merged

Only add the Mockito surefire agent configuration when asked, and keep it minimal#1184
timtebeek merged 7 commits into
mainfrom
mockito-javaagent-less-invasive-and-optin

Conversation

@timtebeek

@timtebeek timtebeek commented Aug 4, 2026

Copy link
Copy Markdown
Member

Summary

  • AddMockitoJavaAgentToMavenSurefirePlugin only adds surefire's @{argLine} late replacement (and the empty argLine property that backs it) when something actually supplies that property
  • The recipe is no longer part of UpgradePluginsForJava25, so UpgradeToJava25 does not add the Mockito agent configuration on its own

Problem

  • Reported via moderneinc/customer-requests#2942. Every module with a test-scoped org.mockito:mockito-core picked up a maven-dependency-plugin properties execution, an empty <argLine/> property, and a surefire <argLine>@{argLine} -javaagent:${org.mockito:mockito-core:jar}</argLine> as a side effect of migrating to Java 25. That is a sizeable, hard-to-unwind diff across a large estate, and it is not something the migration strictly requires: Mockito's Byte Buddy self-attachment still works on current JDKs, JEP 451 only warns about it.

Solution

Only use @{argLine} when it buys something. Surefire's late replacement exists so that a value another plugin sets at build time (in practice JaCoCo's prepare-agent) survives into the test JVM, and it drags along an empty argLine property so builds without such a provider do not pass the literal token to the JVM. Both are now added only when an argLine property is already declared or JaCoCo is present. Poms with neither get:

<argLine>-javaagent:${org.mockito:mockito-core:jar}</argLine>

Output is unchanged when JaCoCo or an existing argLine is in play.

Two details worth calling out:

  • JaCoCo is detected from the raw XML document rather than the resolved model. RawPom.Profile does not parse <build><plugins>, so a JaCoCo declaration that is only active under a profile never reaches ResolvedPom.getPlugins(); going through the resolved model would silently drop coverage for those projects. Scanning the document also picks up pluginManagement. JaCoCo inherited from a parent pom's profile remains undetectable.
  • An existing but empty <argLine/> previously fell back to the literal string @{argLine}. Left as-is that would now emit @{argLine} with no property backing it, so a blank value is treated as if the tag were absent.

Make the recipe opt-in. Removed from UpgradePluginsForJava25. The description now explains that self-attachment still works today and that the recipe is about silencing the JDK warning and staying ahead of the eventual restriction, since users have to seek it out deliberately now.

The trade-off: teams migrating to Java 25 no longer get this hardening automatically and will keep seeing the dynamic-agent-loading warning. Happy to reconsider and keep it bundled with only the first change if that balance seems wrong.

Test plan

  • Existing tests pass (./gradlew build)

  • New tests added: no runtime argLine provider, JaCoCo in build/plugins, JaCoCo only under a profile, and an argLine property already declared

  • UpgradeToJava25Test asserts the agent configuration is no longer added

  • recipes.csv regenerated and examples.yml updated for the new @DocumentExample output

  • Fixes moderneinc/customer-requests#2942

@timtebeek timtebeek added the recipe Recipe requested label Aug 4, 2026
@timtebeek
timtebeek requested a review from MBoegers August 4, 2026 11:43
@timtebeek
timtebeek merged commit 93dc836 into main Aug 4, 2026
1 check passed
@timtebeek
timtebeek deleted the mockito-javaagent-less-invasive-and-optin branch August 4, 2026 20:48
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

recipe Recipe requested

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant