单元测试Maven Mojo-组件和参数为空 [英] Unit Testing Maven Mojo - Components and parameters are null

查看:130
本文介绍了单元测试Maven Mojo-组件和参数为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于文档和邮件线程,我已经看到了3种将Maven项目注入到我的mojo中的方法:

Based on the documentation and mailing threads I have seen 3 ways to inject the maven project into my mojo:

/**
  * Project instance
  * 
  * @parameter default-value="${project}"
  * @required
  * @readonly
  */
private MavenProject project;


@Component
private MavenProject project;


@Parameter(  expression = "${project}" )    
private MavenProject project;

但是无论我选择哪一种,当我根据在maven docs中发现的示例运行单元测试时,project始终为空.

But regardless of which one of these I choose, when I run the unit test based on the example I found in maven docs, project is always null.

public void testMojoGoal() throws Exception {
    File testPom = new File(getBasedir(),
            "src/test/resources/unit/basic-test/sample-sh-project-config.xml");

    ShunitResourcesMojo mojo = (ShunitResourcesMojo) lookupMojo("prepare",testPom);

    assertNotNull(mojo);

    mojo.execute();     
}

mojo execute包含(并且失败)

mojo execute contains (and fails on)

Validate.notNull(project);

推荐答案

我当时正在使用自己的插件,遇到了同样的问题.我找不到有关它的文档,但是我看了一下maven-ant-plugin( https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-ant-plugin ),看看他们是如何做到的.

I was just working on my own plugin and ran into the same problem. I could not find documentation on it, but I had a look at the maven-ant-plugin (https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-ant-plugin) to see how they did it.

我假设这是内置的测试工具行为,但是您必须在测试pom.xml中添加一个项目"参数,以将值设置为MavenProjectStub的子类,例如在"ant-test"测试中:

I assume this is built-in test harness behavior, but you have to add a "project" parameter to your test pom.xml to set the value to a subclass of MavenProjectStub, for example in the "ant-test" test:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ant-plugin</artifactId>
    <configuration>
      <project implementation="org.apache.maven.plugin.ant.stubs.AntTestMavenProjectStub"/>
      <settings implementation="org.apache.maven.plugin.ant.stubs.SettingsStub"/>
      <localRepository>${localRepository}</localRepository>
    </configuration>
  </plugin>

我希望您已经有了答案,只发布这些内容即可.

I hope you had your answer already, just posting for those to follow.

这篇关于单元测试Maven Mojo-组件和参数为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆