如何使用JUnit4测试maven插件

3pvhb19x  于 2023-04-29  发布在  Maven
关注(0)|答案(4)|浏览(179)

我正在写一个maven插件,想写一些JUnit测试。我遵循了Maven Plugin Testing中的描述。不幸的是,在我可以配置或调用任何东西之前,我在测试的设置过程中一直得到一个Exception。
这是我的JUnit测试代码:

public class ResetMojoTest {

    private static final String POM_FILE_NAME = "/path/to/pom.xml"; 

    @Rule
    public MojoRule rule = new MojoRule();

    @Test
    public void testSomething()
        throws Exception
    {
        File pom = new File(POM_FILE_NAME);
        Assert.assertNotNull( pom );
        Assert.assertTrue( pom.exists() );

        ResetMojo resetMojo = (ResetMojo) rule.lookupMojo( "touch", pom );
        Assert.assertNotNull( resetMojo );
        resetMojo.execute();
    }

}

这是异常的堆栈跟踪:

java.io.IOException: Stream closed
    at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:134)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
    at org.apache.commons.io.input.BOMInputStream.getBOM(BOMInputStream.java:175)
    at org.apache.commons.io.input.BOMInputStream.getBOMCharsetName(BOMInputStream.java:201)
    at org.apache.commons.io.input.XmlStreamReader.doRawStream(XmlStreamReader.java:412)
    at org.apache.commons.io.input.XmlStreamReader.<init>(XmlStreamReader.java:206)
    at org.apache.commons.io.input.XmlStreamReader.<init>(XmlStreamReader.java:171)
    at org.apache.commons.io.input.XmlStreamReader.<init>(XmlStreamReader.java:140)
    at org.apache.maven.plugin.testing.AbstractMojoTestCase.setUp(AbstractMojoTestCase.java:119)
    at org.apache.maven.plugin.testing.MojoRule$2.evaluate(MojoRule.java:299)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)

有什么办法能让它工作吗?

5w9g7ksd

5w9g7ksd1#

也遇到了这个问题。mojo规则失败了,因为它示例化了一个AbstractMojoTestCase的匿名实现,然后它会查找哪个,然后尝试创建一个输入流,在这里加载数据:InputStream is = getClass().getResourceAsStream( "/" + getPluginDescriptorLocation() );。这(getPluginDescriptorLocation())只是一个硬编码的文件字符串"META-INF/maven/plugin.xml"
我通过添加以下依赖项来解决这个问题:

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-compat</artifactId>
  <version>3.5.0</version>
</dependency>

我不知道为什么这解决了这个问题。该文件仍然不存在(我检查了原始源代码和target目录,但它都不在)。因此,需要额外的搜索来弄清楚为什么这样做。
下面是我的依赖项以供参考。

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-plugin-api</artifactId>
  <version>3.5.0</version>
</dependency>
<dependency>
  <groupId>org.twdata.maven</groupId>
  <artifactId>mojo-executor</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>org.apache.maven.plugin-tools</groupId>
  <artifactId>maven-plugin-annotations</artifactId>
  <version>3.5</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.eclipse.aether</groupId>
  <artifactId>aether-api</artifactId>
  <version>1.1.0</version>
</dependency>
<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-core</artifactId>
  <version>3.5.0</version>
</dependency>
<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-compat</artifactId>
  <version>3.5.0</version>
</dependency>
<!--Test Dependencies-->
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.apache.maven.plugin-testing</groupId>
  <artifactId>maven-plugin-testing-harness</artifactId>
  <version>3.3.0</version>
  <scope>test</scope>
</dependency>
nxowjjhe

nxowjjhe2#

我也有同样的问题。通过添加正确的依赖关系解决了它。重要的是,标记为由harness maven插件提供的依赖项被添加到测试范围:

<properties>
  <dependency.maven.version>3.3.9</dependency.maven.version>
  <!-- and others -->
</properties>

<dependencies>
  <!-- your dependencies -->

  <!-- maven plugin dependencies -->
  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>${dependency.maven.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-core</artifactId>
    <version>${dependency.maven.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-model</artifactId>
    <version>${dependency.maven.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-artifact</artifactId>
    <version>${dependency.maven.version}</version>
  </dependency>

  <!-- test dependencies -->
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${dependency.junit.version}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.maven.plugin-testing</groupId>
    <artifactId>maven-plugin-testing-harness</artifactId>
    <version>3.3.0</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-aether-provider</artifactId>
    <version>${dependency.maven.version}</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-compat</artifactId>
    <version>${dependency.maven.version}</version>
    <scope>test</scope>
  </dependency>

  <!-- provided maven dependencies -->
  <dependency>
    <groupId>org.apache.maven.plugin-tools</groupId>
    <artifactId>maven-plugin-annotations</artifactId>
    <version>3.3</version>
    <scope>provided</scope>
  </dependency>

</dependencies>

您的Test类应该如下所示

public class YourMojoTest {

  @Rule
  public MojoRule rule = new MojoRule();

  @Test
  public void shouldExecuteMojo() throws Exception {
    // given
    File testPom = new File(PlexusTestCase.getBasedir(), "src/test/resources/pom.xml");

    final Properties properties = new Properties();
    final MavenProject mavenProject = Mockito.mock(MavenProject.class);
    Mockito.when(mavenProject.getProperties()).thenReturn(properties);

    // when
    YourMojo mojo = (YourMojo) rule.lookupMojo("fetch", testPom);
    assertNotNull(mojo);
    mojo.setProject(mavenProject);
    mojo.execute();

    // then
    // do your tests ...
  }
}

希望这有助于其他挣扎在同样的问题

x6492ojm

x6492ojm3#

通过添加maven兼容性库修复了此问题:-

<dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-compat</artifactId>
            <version>${maven.version}</version>
            <scope>test</scope>
        </dependency>
luaexgnf

luaexgnf4#

POM文件的加载看起来不太好:
下面的代码来自示例:

File pom = rule.getTestFile( "src/test/resources/unit/project-to-test/pom.xml" );

这就是你所拥有的

File pom = new File(POM_FILE_NAME);

除此之外,您还有一个不同的位置,看起来像这样:

private static final String POM_FILE_NAME = "/path/to/pom.xml";

但是pom文件的位置应该是:

private static final String POM_FILE_NAME = "src/test/resources/pom.xml";

相关问题