Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
How to make MavenProject injected into the mojo during test lookup?

This is my test (maven-plugin-testing-harness 3.3.0, junit 5.6.2):

import java.io.File;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public final class MyMojoTest extends AbstractMojoTestCase {
  @BeforeEach
  public void setup() throws Exception {
    this.setUp();
  }
  @Test
  public void executeIt() throws Exception {
   final File pom = new File("src/test/resources/my-test-pom.xml");
    final MyMojo mojo = MyMojo.class.cast(
      this.lookupMojo("mygoal", pom)
    );
    mojo.execute();
  }
}

This is what I have in MyMojo (maven-plugin-api 3.8.4):

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

@Mojo(name = "my", defaultPhase = LifecyclePhase.COMPILE)
public final class MyMojo extends AbstractMojo {
  @Parameter(defaultValue = "${project}", readonly = true)
  private MavenProject project;
}

The problem is that mojo returned by lookupMojo() doesn't have the project attribute set (it's null).

Some solution was proposed here, but I'm not sure how it can work with JUnit 5.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Possible solutions

Solution #1: Use AbstractMojoTestCase.lookupConfiguredMojo() method

Please, consider the implementation of the test class as an example: maven-plugin-testing/ParametersMojoTest.java at maven-plugin-testing-3.3.0 · apache/maven-plugin-testing.

Considering this example, please, note the Mojo instantiation approach:

  1. The readMavenProject() method.

  2. The Mojo instantiation uses the readMavenProject() and lookupConfiguredMojo() methods:

    MavenProject project = readMavenProject( new File( "src/test/projects/default" ) );
    
    ParametersMojo mojo = (ParametersMojo) lookupConfiguredMojo( project, "parameters" );
    

This Mojo instantiation approach provides the instantiated Mojo with the correct MavenProject parameter value.

Some additional references

  • Related Stack Overflow answer.

Solution #2: Test pom.xml for plugin: Use project stub

It is necessary to update the test pom.xml file by introducing the project element (of the configuration element) with the stub.

For example:

<project>

    <…>

    <build>
        <plugins>
            <plugin>
                <artifactId>touch-maven-plugin</artifactId>
                <configuration>
                    <project implementation="org.apache.maven.plugin.testing.stubs.MavenProjectStub">
                        <groupId implementation="java.lang.String">test-group-id</groupId>
                        <artifactId implementation="java.lang.String">test-artifact-id</artifactId>
                        <version implementation="java.lang.String">1.0.0-SNAPSHOT</version>
                    </project>

                    <…>

                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Some additional references

  • An example. maven-jar-plugin/pom.xml at maven-jar-plugin-3.2.2 · apache/maven-jar-plugin.
  • Related Stack Overflow answer.
over 4 years ago · Santiago Trujillo Relatório

0

I tried with the same configurations as mentioned above. The plugin works fine but none of the tests having lookupMojo() seems to be working.

A similar test example can be referred here. There is a difference in the setUp method from your class MyMojoTest and example provided in the link.
super.setUp(); should be called instead of this.setUp() so as to initialize all the objects in AbstractMojoTestCase class.

The possible reason that the test case with maven-plugin-testing-harness 3.3.0 and junit 5.6.2 will not work because they are not compatible.
The reasons are

  1. maven-plugin-testing-harness was built to be compatible with Junit4. Latest update was a long time ago i.e. Dec 17, 2014. Junit 4 and Junit 5 are not compatible. We have to make use of Junit-Vintage-Engine to make it work.
  2. maven-plugin-testing-harness was develop using JDk-7 and minimum requirements for Junit 5 is Jdk-8. Information from the harness plugin Manifest file

Implementation-Vendor-Id: org.apache.maven.plugin-testing
Built-By: igor
Build-Jdk: 1.7.0_55
Specification-Vendor: The Apache Software
Foundation Specification-Title: Maven Plugin Testing Mechanism

  1. Maven version supported is also different for both the jars. link

Few other links confirm the same.

There are very few libraries and informational link available for plugin testing with Junit5. I could find only a handful of them, although I haven't tried them yet.

Library:

<dependency>
    <groupId>com.soebes.itf.jupiter.extension</groupId>
    <artifactId>itf-assertj</artifactId>
    <version>0.11.0</version>
    <scope>test</scope>
</dependency>

Few more Jupiter extension libraries in this link

Examples related to it.

  • Example 1
  • Example 2
  • Example 3
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda