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

问题是lookupMojo()返回的mojo没有设置project属性(它是null).

有人提出了一些解决方案here,但我不确定它如何与JUnit5一起工作.

推荐答案

我try 了与上述相同的配置.该插件运行良好,但lookupMojo()个测试似乎都不起作用.

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-harnessJunit4兼容.最新的更新发生在time年前,即2014年12月17日.Junit 4Junit 5不兼容.我们必须利用Junit-Vintage-Engine来让它工作.
  2. maven-plugin-testing-harness是使用JDk-7开发的,Junit 5的最低要求是Jdk-8. harness插件 list 文件中的信息

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. 两种JAR支持的Maven版本也不同.

几乎没有其他links家公司证实了这一点.

对于Junit5的插件测试,可用的库和信息链接非常少.虽然我还没有试过,但我只能找到少数几个.

图书馆:

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

在这个link版本中没有更多的jupiter扩展库

与之相关的例子.

Java相关问答推荐

如何计算内循环的时间复杂度?

使用包私有构造函数强制子类Java类

Springdoc Whitelabel Error Page with Spring V3

如何在Javascript中设置文本区域圆角的样式

RESTful框架类字段是安全的还是不安全的

在Java Swing Paint应用程序中捕获快速鼠标移动时遇到困难

由于我在Main方法中关闭了 scanner ,但在该方法中创建了一个新的 scanner ,因此出现了错误

PDFBox未加载内容

在Eclipse中数组的可空性

如何在一行中使用Dijkstra中的Java Stream

Spring-Boot Kafka应用程序到GraalVM本机映像-找不到org.apache.kafka.streams.processor.internals.DefaultKafkaClientSupplier

Spring安全令牌刷新和JWT签名与本地计算的签名不匹配

Groovy/Java:匹配带引号的命令选项

Java堆中的许多java.time.ZoneRegion实例.ZoneId实例不应该被缓存吗?

AbstractList保证溢出到其他方法

在Java中使用StorageReference将数据从Firebase存储添加到数组列表

JavaFX:为什么我的ComboBox添加了一个不必要的单元格的一部分?

java.lang.NoSuchMethodError:';org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.poi-poi-ooxml-5.2.4

ResponseEntity.控制器截断响应的JSON部分

这是JavaFX SceneBuilder的错误吗?