6

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

William
  • 20,150
  • 8
  • 49
  • 91

1 Answers1

10

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

Verhagen
  • 3,885
  • 26
  • 36
GuiSim
  • 7,361
  • 6
  • 40
  • 50
  • i know this is an old thread, but i am very frustrated on this thing. I am following the same procedures and my parameters are not populated form the POM, my project is and the mojo loads properly, but the params are not being picked up. did you encounter anything like that? – Dayan Moreno Leon Jan 14 '19 at 20:03
  • @DayanMorenoLeon I wish I knew exactly why that was happening to you :( If this doesn't work, you can maybe try to look at the linked github repo and try to reproduce the issue or spot the difference with your own repo? – GuiSim Jan 15 '19 at 18:43
  • 1
    Thank you, actually i was looking for this comment to delete it, thank you. it was a stupid typo on the groupID. – Dayan Moreno Leon Jan 15 '19 at 20:51
  • @DayanMorenoLeon glad you got it resolved! Dealing with Maven plugins is rarely simple or clear. Have a good day! – GuiSim Jan 15 '19 at 20:57
  • 1
    tell me about it, right now i am getting my @$$ kicked by maven-plugin-plugin descriptor because it fails if i include `checkstyle` as a dependency even if is scoped as test – Dayan Moreno Leon Jan 15 '19 at 21:15