How to populate parameter “defaultValue” in Maven “AbstractMojoTestCase”?

一曲冷凌霜 提交于 2019-12-21 16:56:59

问题


I have a Maven plugin that I am attempting to test using a subclass of the AbstractMojoTestCase. The plugin Mojo defines an outputFolder parameter with a defaultValue. This parameter is not generally expected to be provided by the user in the POM.

@Parameter(defaultValue = "${project.build.directory}/someOutputFolder")
private File outputFolder;

And if I use the plugin in a real scenario then the outputFolder gets defaulted as expected.

But if I test the Mojo using the AbstractMojoTestCase then while parameters defined in the test POM are populated, parameters with a defaultValue that are not defined in the POM are not populated.

public class MyPluginTestCase extends AbstractMojoTestCase {

    public void testAssembly() throws Exception {
        final File pom = getTestFile( "src/test/resources/test-pom.xml");
        assertNotNull(pom);
        assertTrue(pom.exists());

        final MyMojo myMojo = (BaselineAssemblyMojo) lookupMojo("assemble", pom);
        assertNotNull(myMojo);
        myMojo.execute(); // Dies due to NullPointerException on outputFolder.
    }
}

Further: if I define the outputFolder parameter in the POM like so:

<outputFolder>${project.build.directory}/someOutputFolder</outputFolder>

then ${project.build.directory} is NOT resolved within the AbstractMojoTestCase.

So what do I need to do to get the defaultvalue populated when testing?

Or is this a fault in the AbstractMojoTestCase?

This is Maven-3.2.3, maven-plugin-plugin-3.2, JDK 8


回答1:


You need to use lookupConfiguredMojo.

Here's what I ended up using:

public class MyPluginTest
{    
    @Rule
    public MojoRule mojoRule = new MojoRule();

    @Test
    public void noSource() throws Exception
    {
        // Just give the location, where the pom.xml is located
        MyPlugin plugin = (MyPlugin) mojoRule.lookupConfiguredMojo(getResourcesFile("basic-test"), "myGoal");
        plugin.execute();

        assertThat(plugin.getSomeInformation()).isEmpty();
    }

    public File getResourcesFile(String filename)
    {
        return new File("src/test/resources", filename);
    }
}

Of course you need to replace myGoal with your plugin's goal. You also need to figure out how to assert that your plugin executed successfully.

For a more complete example, check out the tests I wrote for fmt-maven-plugin



来源:https://stackoverflow.com/questions/31528763/how-to-populate-parameter-defaultvalue-in-maven-abstractmojotestcase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!