Check the default values ​​and Mojos expressions with Maven Plugin Testing Harness:

I have a problem using Maven Plugin Testing Harness (2.0-alpha1): When I want to test my Mojo, the default values ​​and expressions for the 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 zero. I tried to introduce a MavenProjectStub that returns ${project.build.directory} successfully, but this does not apply to my Mojo parameter.

Is there a way to include default values ​​and expressions like ${project.build.directory} inside my Mojos during tests?

+4
source share
4 answers

So it looks like they added lookupConfiguredMojo just for this use case . It took me a while to figure out what to call it, because you need a properly configured MavenProject to use it. Here is 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"); ... 
+7
source

I hit a bunch of problems including this while testing my plugin. For each problem, it took only a few lines, but finding them was not easy. Pointers here helped!

I combined them in the BetterAbstractMojoTestCase.java class. It contains magical lines that solved the half-dozen problems that I hit; and it gives the lookupConfiguredMojo(File pom, String goal) method for your problem here.

+3
source

I had the same problem, and I could not find a solution, so I decided to fix it myself. I checked the source code for the latest version of maven-plugin-test-harness (which is now 2.0-alpha-1) and put it in my own github repository.

You will need to check the code and create it locally.

The only change you should make to your project is to replace the dependencies in your POM. I used my own domain / groupId instead of Apache to avoid conflicts (and confusion) with future versions of Apache.

This is what you need to put in 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 get the code from: https://github.com/grighetto/maven-plugin-testing-harness

+2
source

For version 3+:

  <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <version>3.5</version> <scope>provided</scope> </dependency> 

took some time to embarrass this myself before realizing that I was mixing annotation syntax with javadoc syntax.

 @Parameter(property = "project.build.directory") private String projectBuildDir; 

Then connect the file name with this value to complete the path in your code.

0
source

Source: https://habr.com/ru/post/1398978/


All Articles