Skip to content

[MNG-11449] Add Mockito javaagent for maven-cli tests#11743

Open
arturobernalg wants to merge 1 commit into
apache:masterfrom
arturobernalg:11449
Open

[MNG-11449] Add Mockito javaagent for maven-cli tests#11743
arturobernalg wants to merge 1 commit into
apache:masterfrom
arturobernalg:11449

Conversation

@arturobernalg

Copy link
Copy Markdown
Member

Configure surefire in impl/maven-cli to pass Mockito as a Java agent

Following this checklist to help us incorporate your
contribution quickly and easily:

  • [ X] Your pull request should address just one issue, without pulling in other changes.
  • [X ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • [ X] 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.
  • [ X] 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.
  • [X ] Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • [X ] 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.

Configure surefire in impl/maven-cli to pass Mockito as a Java agent

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

Claude Code on behalf of Guillaume Nodet

The parent pom already has a mechanism for this: maven-dependency-plugin:properties sets org.mockito:mockito-core:jar, which activates the mockito profile that adds -javaagent:${org.mockito:mockito-core:jar} to surefire's argLine (lines 942-961 of the root pom).

If that mechanism isn't working for maven-cli, it would be better to fix the root cause rather than hardcoding the argLine in this module. Note that the mockito profile in the parent pom (line 955) itself has a pre-existing issue: it sets <argLine>-Xmx256m -javaagent:${org.mockito:mockito-core:jar}</argLine>, which drops @{jacocoArgLine} compared to the default (line 700). That means JaCoCo coverage is silently broken whenever the mockito profile activates.

Could you check why the parent-level profile doesn't activate for maven-cli? The module already has mockito-core as a test dependency, so the properties goal should set the property and the profile should activate.

Also, MockitoAgentConfigurationTest is a test that only verifies build infrastructure configuration — it doesn't test any production code. If the javaagent is missing, Mockito-based tests would fail with clear errors anyway, making this test redundant.

Comment thread impl/maven-cli/pom.xml
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx256m @{jacocoArgLine} -javaagent:${org.mockito:mockito-core:jar}</argLine>

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.

This hardcodes the argLine at the module level, bypassing the parent pom's mockito profile mechanism (root pom lines 942-961). The parent already has maven-dependency-plugin:properties + a profile that adds -javaagent:${org.mockito:mockito-core:jar} to surefire.

If the parent mechanism doesn't work here, it would be better to fix it at the parent level. Also note that the parent's mockito profile (line 955) itself drops @{jacocoArgLine} — that's a pre-existing bug worth fixing regardless.

inputArguments.stream().anyMatch(a -> a.startsWith("-javaagent:") && a.contains("mockito-core")),
() -> "Expected Mockito javaagent in JVM arguments, but got: " + inputArguments);
}
}

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.

This test only verifies build infrastructure (that the javaagent JVM flag is present). It doesn't test any production code. If the mockito agent were missing, any test using Mockito.mock() on a non-interface would already fail with a clear error, making this test redundant.

I'd suggest removing this file — the real fix is just the surefire configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants