Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test default values and expressions of Mojos using Maven Plugin Testing Harness:

I have a problem using the Maven Plugin Testing Harness (2.0-alpha1): When I want to test my Mojo, the default values and expressions for parameters are not applicable. I have the following parameter:

/**
 * <p>The output file to write the settings to.</p>
 *
 * @parameter default-value="${project.build.directory}/myProperties.properties" expression="${properties.file}"
 */

private String file;

When I run my unit tests this property is always null. I tried to inject a MavenProjectStub which returns ${project.build.directory} successfully but this is not applied to my Mojo parameter.

Is there any way to enable default values and expressions like ${project.build.directory} inside my Mojos during the tests?

like image 485
Daniel S. Avatar asked Feb 29 '12 09:02

Daniel S.


3 Answers

So it looks like they added lookupConfiguredMojo for just this use case. It took me a while to figure out how to call that because you need a properly configured MavenProject to use it. Here's what worked for me:

File pomFile = ...

MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest();
ProjectBuilder projectBuilder = this.lookup(ProjectBuilder.class);
MavenProject project = projectBuilder.build(pomFile, buildingRequest).getProject();

MyMojo mojo = (MyMojo) this.lookupConfiguredMojo(project, "my-goal");
...
like image 133
Steve Avatar answered Sep 28 '22 01:09

Steve


I hit a bunch of issues including this when testing my plugin. Each problem only took a few lines to fix but finding them wasn't easy. The pointers here helped!

I've combined them in a BetterAbstractMojoTestCase.java class. It contains the magic lines which solved the half-dozed issues I hit; and it gives a lookupConfiguredMojo(File pom, String goal) method for your issue here.

like image 24
Partly Cloudy Avatar answered Sep 28 '22 02:09

Partly Cloudy


I've had this very same problem and couldn't find any solution, so I decided to fix it myself. I've checked out the source code for the latest version of the maven-plugin-testing-harness (which is 2.0-alpha-1 at the moment) and placed it in my own github repository.

You will have to checkout the code from there and build it locally.

The only change you need to make in your project is replace the dependency in your POM. I used my own domain/groupId name instead of the Apache one just to avoid any conflicts (and confusion) with future Apache releases.

This is what you need to put in your POM:

<dependency>
    <groupId>com.menttis.maven.plugin-testing</groupId>
    <artifactId>maven-plugin-testing-harness</artifactId>
    <version>2.0.1</version>
    <scope>test</scope>
</dependency>

And this is the repository where you can grab the code from: https://github.com/grighetto/maven-plugin-testing-harness

like image 29
Gianluca Righetto Avatar answered Sep 28 '22 01:09

Gianluca Righetto