如何使用JUnit4测试maven插件

时间:2014-06-18 06:26:34

标签: java maven maven-plugin junit4

我正在写一个maven插件,想写一些JUnit测试。我按照Maven Plugin Testing中的说明进行操作。不幸的是,在配置或调用任何内容之前,我在测试设置期间一直遇到异常。

这是我的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)

有关如何使其发挥作用的任何想法?

3 个答案:

答案 0 :(得分:1)

也遇到了这个问题。 mojo规则失败,因为它实例化了一个window.location.href = forwardedWebsiteLink + "?" + "#first-drop"; 的匿名实现,然后查找然后尝试创建一个输入流来加载数据: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

答案 1 :(得分:0)

遇到同样的问题。通过添加正确的依赖项来解决它。重要的是,标记为由线束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 ...
  }
}

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

答案 2 :(得分:-1)

加载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";