`AddMockitoJavaAgentToMavenSurefirePlugin` adds the agent when a parent already loads it from an explicit jar path

Abierto
#1,383 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
java
Área
build-system

Línea de trabajo

Start with AddMockitoJavaAgentToMavenSurefirePlugin.java, especially the surefireArgLineWithAgent check, and trace how it handles inherited Surefire configuration and the dependency-plugin execution requirement. Add a regression test based on the parent-and-child reproduction in the issue. Done means an inherited explicit Mockito agent path prevents redundant recipe changes and preserves the parent's Surefire arguments.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug

What version of OpenRewrite are you using?

  • rewrite-migrate-java 3.49.0
  • rewrite-maven-plugin 6.53.0

How are you running OpenRewrite?

Through the Maven plugin, with org.openrewrite.java.migrate.UpgradeToJava25 in a multi-module project. All modules inherit from a corporate parent POM.

What is the smallest, simplest way to reproduce the problem?

The parent POM sets up the Mockito agent once for every project that inherits it. It refers to the jar by its path in the local repository, not by ${org.mockito:mockito-core:jar}:

<properties>
    <argLine/>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>
                    @{argLine}
                    -javaagent:${settings.localRepository}/org/mockito/mockito-core/${mockito.version}/mockito-core-${mockito.version}.jar
                </argLine>
            </configuration>
        </plugin>
    </plugins>
</build>

A child module uses the parent and has org.mockito:mockito-core as a test dependency, with no surefire configuration of its own.

@Test
void agentInheritedFromParentAsExplicitJarPath() {
    rewriteRun(
      mavenProject("parent",
        pomXml(/* the parent above, with mockito.version set */),
        mavenProject("child",
          pomXml(/* child with mockito-core test dependency, no surefire config */)
        )
      )
    );
}

What did you expect to see?

No changes. Every module already runs its tests with the Mockito agent, inherited from the parent's build/plugins.

What did you see instead?

The recipe changes every module that uses mockito-core:

  • it adds a maven-surefire-plugin with <argLine>-javaagent:${org.mockito:mockito-core:jar}</argLine> (or @{argLine} -javaagent:...);
  • it adds a maven-dependency-plugin properties execution (get-mockito-agent-path).

The module's own <argLine> replaces the inherited one, so each module loses the rest of the parent's surefire arguments (here --add-opens and the @{argLine}/JaCoCo chaining).

Why it happens

The parent check added in #1166 (for #1164) only treats the agent as configured when the inherited argLine contains the exact text the recipe would add (AddMockitoJavaAgentToMavenSurefirePlugin.java, surefireArgLineWithAgent):

.filter(argLine -> argLine != null && argLine.contains(agentArgument))

agentArgument is -javaagent:${org.mockito:mockito-core:jar} (or the byte-buddy-agent form). It also requires an inherited maven-dependency-plugin properties execution. That execution is only needed for the ${group:artifact:jar} form.

Why a parent would use the explicit path

${org.mockito:mockito-core:jar} is only set by maven-dependency-plugin:properties in modules that depend on mockito-core. In a corporate parent that applies one surefire configuration to every project, a module without mockito-core gets an unresolved -javaagent:${org.mockito:mockito-core:jar}, and its test JVM fails to start. Referring to the jar by its path, with the version Spring Boot's BOM manages, avoids that.

Suggested fix

Treat the agent as already configured when any inherited or local surefire argLine has a -javaagent: argument that points at a Mockito or Byte Buddy agent jar, in whatever form. For example, match -javaagent:\S*(mockito-core|byte-buddy-agent)\S*, not only the exact ${...:jar} text. When that agent is not the ${...:jar} form, don't require the maven-dependency-plugin properties execution either.

Are you interested in contributing a fix to OpenRewrite?

Yes, I'm happy to open a PR with the test above and the change to surefireArgLineWithAgent.

Lenguaje dominante
Java
Estrellas
162
Forks
139
Merge medio
10 h 28 min
PR fusionados (30 d)
121

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de openrewrite/rewrite-migrate-java

Todos los issues de openrewrite/rewrite-migrate-java

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.