如何在测试查找期间将 MavenProject 注入到 mojo 中?

2023-12-23

这是我的测试(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();
  }
}

这就是我所拥有的MyMojo(maven-插件-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;
}

问题是mojo由返回lookupMojo()没有project属性集(它是null).

提出了一些解决方案here https://stackoverflow.com/questions/31528763/how-to-populate-parameter-defaultvalue-in-maven-abstractmojotestcase,但我不确定它如何与 JUnit 5 一起工作。


我尝试使用与上面提到的相同的配置。该插件工作正常,但没有一个测试有lookupMojo()似乎正在发挥作用。

类似的测试例子可以参考here https://maven.apache.org/plugin-testing/maven-plugin-testing-harness/getting-started/index.html。存在差异setUp你班级的方法MyMojoTest以及链接中提供的示例。
super.setUp();应该调用而不是this.setUp()从而初始化所有的对象AbstractMojoTestCase class.

测试用例的可能原因是maven-plugin-testing-harness 3.3.0 and junit 5.6.2不会起作用,因为它们不是compatible.
原因是

  1. maven-plugin-testing-harness旨在兼容Junit4。最新更新有点长time https://mvnrepository.com/artifact/org.apache.maven.plugin-testing/maven-plugin-testing-harness/3.3.0之前,即 2014 年 12 月 17 日。Junit 4 and Junit 5不兼容。我们必须利用Junit-Vintage-Engine使其发挥作用。
  2. maven-plugin-testing-harness是使用开发的JDk-7和最低要求Junit 5 is Jdk-8。 信息来自harness插件清单文件

实现供应商 ID:org.apache.maven.plugin-testing
建造者:伊戈尔
构建Jdk:1.7.0_55
规范供应商:Apache 软件
基础规范-标题:Maven 插件测试机制

  1. 这两个 jar 支持的 Maven 版本也不同。link https://github.com/apache/maven-plugin-testing/blob/master/maven-plugin-testing-harness/src/site/fml/faq.fml

其他的很少links https://khmarbaise.github.io/maven-it-extension/itf-documentation/background/background.html确认相同。

可用于 Junit5 插件测试的库和信息链接非常少。尽管我还没有尝试过,但我只能找到其中的一小部分。

Library:

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

这里还有一些 Jupiter 扩展库link https://mvnrepository.com/artifact/com.soebes.itf.jupiter.extension

与之相关的例子。

  • 实施例1 https://dzone.com/articles/maven-plugin-testing-in-a-modern-way-part-i
  • 实施例2 https://dzone.com/articles/maven-plugin-testing-in-a-modern-way-part-iv
  • 实施例3 https://dzone.com/articles/maven-plugin-testing-in-a-modern-way-part-ii
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在测试查找期间将 MavenProject 注入到 mojo 中? 的相关文章

随机推荐