Skip to content

[#11449] Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin#12369

Open
Hiteshsai007 wants to merge 2 commits into
apache:masterfrom
Hiteshsai007:maven-11449-mockito-agent
Open

[#11449] Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin#12369
Hiteshsai007 wants to merge 2 commits into
apache:masterfrom
Hiteshsai007:maven-11449-mockito-agent

Conversation

@Hiteshsai007

@Hiteshsai007 Hiteshsai007 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

The mockito profile activated by dependency:properties used Maven property interpolation () which resolves at POM parse time - before the dependency:properties goal runs and sets the org.mockito:mockito-core:jar property. This caused the javaagent path to remain unresolved.

Fix: use Surefire/Failsafe late-binding property interpolation (@{...}) which resolves the property at test execution time, after dependency:properties has already set it.

Also:

  • Restore @{jacocoArgLine} placeholder (was dropped in previous attempt)
  • Extend the profile to cover maven-failsafe-plugin as well

Fixes: #11449

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][core-its] 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.

… extend to failsafe plugin

The mockito profile activated by dependency:properties used Maven property
interpolation (\) which resolves at POM parse time - before the
dependency:properties goal runs and sets the org.mockito:mockito-core:jar
property. This caused the javaagent path to remain unresolved.

Fix: use Surefire/Failsafe late-binding property interpolation (@{...})
which resolves the property at test execution time, after dependency:properties
has already set it.

Also:
- Restore @{jacocoArgLine} placeholder (was dropped in previous attempt)
- Extend the profile to cover maven-failsafe-plugin as well

Fixes: https://issues.apache.org/jira/browse/MNG-11449
@Hiteshsai007 Hiteshsai007 changed the title [MNG-11449] Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin #11449 Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin Jun 25, 2026
@Hiteshsai007 Hiteshsai007 changed the title #11449 Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin https://github.com/apache/maven/issues/11449 Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin Jun 25, 2026
@Hiteshsai007 Hiteshsai007 changed the title https://github.com/apache/maven/issues/11449 Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin #11449 Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin Jun 25, 2026
@Hiteshsai007 Hiteshsai007 changed the title #11449 Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin [#11449] Fix Mockito agent: use late binding @{} interpolation and extend to failsafe plugin Jun 25, 2026
@gnodet gnodet requested a review from Copilot June 26, 2026 14:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Updates the root build’s mockito profile so the Mockito -javaagent path is resolved at test execution time (after dependency:properties has populated org.mockito:mockito-core:jar), and applies the same behavior consistently to both unit tests (Surefire) and integration tests (Failsafe).

Changes:

  • Switch Mockito agent path interpolation from Maven parse-time (${...}) to Surefire/Failsafe late-binding (@{...}).
  • Restore inclusion of @{jacocoArgLine} in the profile’s argLine.
  • Add equivalent argLine configuration for maven-failsafe-plugin under the mockito profile.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gnodet gnodet left a comment

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.

Review — Fix Mockito agent: use late-binding @{} interpolation and extend to failsafe plugin

What the PR does: This 1-file, 8-line change fixes the mockito Maven profile so the Mockito Java agent path resolves correctly at test execution time rather than at POM parse time. It switches the surefire <argLine> from ${org.mockito:mockito-core:jar} (eager, parse-time interpolation) to @{org.mockito:mockito-core:jar} (Surefire/Failsafe late-binding, resolves after dependency:properties has run). It also restores the @{jacocoArgLine} placeholder that the old mockito-profile argLine was missing, and extends the configuration to maven-failsafe-plugin for integration tests.

Verdict: The change looks correct. The fix aligns with how @{jacocoArgLine} is already used throughout this POM (lines 700, 711, 1220) and properly handles the case where jacocoArgLine is empty (it's defaulted to empty at line 174). The pluginManagement placement is consistent with the existing surefire entry in the profile, and Maven's configuration merge semantics mean only <argLine> is overridden — the <forkNode> and <systemPropertyVariables> from the default pluginManagement are inherited correctly.

Findings

No confirmed or plausible bugs were found in this PR's changes.

Pre-existing note (not introduced by this PR)

pom.xml:1220 — When both the jacoco and mockito profiles are active simultaneously, the jacoco profile's direct <build><plugins> surefire config (<argLine>-Xmx1G @{jacocoArgLine}</argLine>) takes precedence over the mockito profile's <build><pluginManagement> surefire config, causing the -javaagent:@{org.mockito:mockito-core:jar} to be silently dropped for unit tests. This was already the case before this PR (the old ${...} syntax had the same override behavior), so it's not a regression — but it may be worth a follow-up if both profiles are expected to be used together. Failsafe is unaffected since the jacoco profile doesn't override failsafe's argLine.


🤖 Generated with Claude Code

When both jacoco and mockito profiles were active, the jacoco profile's surefire configuration overrode the mockito profile's surefire pluginManagement configuration. This caused the mockito javaagent to be silently dropped for unit tests.

Fix: Refactor the surefire and failsafe argLine to be composed dynamically via properties:
- Define argLine.xmx (default: -Xmx256m) and argLine.mockito (default: empty) in the root POM properties.
- Update default pluginManagement configuration for surefire/failsafe to: \ @{jacocoArgLine} \
- Modify the mockito profile to only set argLine.mockito property to the mockito javaagent.
- Modify the jacoco profile to only override argLine.xmx property to -Xmx1G and remove the direct surefire argLine override.

This ensures both profiles can be active simultaneously without overriding each other's configurations.
@Hiteshsai007

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've updated the PR to resolve the profile collision highlighted in the pre-existing note:

  • The Issue: When both jacoco and mockito profiles were active, the jacoco profile's direct surefire plugin configuration overrode the mockito profile's pluginManagement configuration, dropping the Mockito -javaagent.
  • The Fix: Refactored the surefire and failsafe argLine in pom.xml to be dynamically composed using properties:
    • Added default <argLine.xmx>-Xmx256m</argLine.xmx> and <argLine.mockito /> properties.
    • Set the default plugin configuration to: ${argLine.xmx} @{jacocoArgLine} ${argLine.mockito}.
    • The mockito profile now only defines the <argLine.mockito> property.
    • The jacoco profile now only overrides <argLine.xmx> to -Xmx1G (and no longer overrides the plugin's argLine configuration directly).

This ensures both profiles can be active at the same time without conflicting or dropping each other's parameters.

@slachiewicz slachiewicz added the bug Something isn't working label Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Mockito as an agent

4 participants