[MNG-11449] Add Mockito javaagent for maven-cli tests#11743
[MNG-11449] Add Mockito javaagent for maven-cli tests#11743arturobernalg wants to merge 1 commit into
Conversation
Configure surefire in impl/maven-cli to pass Mockito as a Java agent
gnodet
left a comment
There was a problem hiding this comment.
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.
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <configuration> | ||
| <argLine>-Xmx256m @{jacocoArgLine} -javaagent:${org.mockito:mockito-core:jar}</argLine> |
There was a problem hiding this comment.
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); | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
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:
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.