gnodet commented on PR #2055:
URL: https://github.com/apache/maven/pull/2055#issuecomment-2611138726

   > I'd like to share a few thoughts that I have during writing the tests (if 
the mailing list is a better place, I can also share them there):
   > 
   > * During testing I miss a possibility where I can inject mocks to the mojo.
   > * Is it possible to introduce constructor injection? It would be easier to 
write mojo test with plain JUnit5. I know that I can add a constructor with 
parameters, but then I also have to a default constructor because of the DI 
framework. It feels noisy.
   > 
   > This construction
   > 
   > ```java
   >    @Inject
   >     public HelloMojo(MavenDIComponent component) {
   >         this.component = component;
   >     }
   > ```
   > 
   > results in runtime error:
   > 
   > ```shell
   > org.junit.jupiter.api.extension.ParameterResolutionException: Unable to 
resolve mojo
   >         at 
org.apache.maven.api.plugin.testing.MojoExtension.resolveParameter(MojoExtension.java:201)
   >         at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
   >         at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
   > Caused by: org.apache.maven.di.impl.DIException: Error while initializing 
binding 
BindingToConstructor[@Named("org.apache.maven.plugins:mavendi-maven-plugin:0.0.1-SNAPSHOT:hello")
 HelloMojoFactory][]
   >         at 
org.apache.maven.di.impl.Binding$1.lambda$compile$0(Binding.java:109)
   >         at 
org.apache.maven.di.impl.InjectorImpl.getInstance(InjectorImpl.java:75)
   >         at 
org.apache.maven.api.di.testing.MavenDIExtension.lookup(MavenDIExtension.java:99)
   >         at 
org.apache.maven.api.plugin.testing.MojoExtension.resolveParameter(MojoExtension.java:181)
   >         ... 2 more
   > Caused by: org.apache.maven.di.impl.DIException: Failed to call 
constructor public org.apache.maven.plugins.HelloMojoFactory() to provide 
requested key 
@Named("org.apache.maven.plugins:mavendi-maven-plugin:0.0.1-SNAPSHOT:hello") 
HelloMojoFactory
   >         at 
org.apache.maven.di.impl.ReflectionUtils.lambda$bindingFromConstructor$7(ReflectionUtils.java:373)
   >         at 
org.apache.maven.di.impl.Binding$BindingToConstructor.lambda$compile$0(Binding.java:188)
   >         at 
org.apache.maven.di.impl.Binding$1.lambda$compile$0(Binding.java:105)
   >         ... 5 more
   > Caused by: java.lang.NoSuchMethodError: 
org.apache.maven.plugins.HelloMojo: method 'void <init>()' not found
   >         at 
org.apache.maven.plugins.HelloMojoFactory.<init>(HelloMojoFactory.java)
   >         at 
java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
   >         at 
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
   >         at 
org.apache.maven.di.impl.ReflectionUtils.lambda$bindingFromConstructor$7(ReflectionUtils.java:360)
   >         ... 7 more
   > ```
   
   It is possible to inject a mock component into the mojo in unit test.
   See how it can be done at:
     
https://github.com/apache/maven-install-plugin/blob/master/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java#L162-L172
     That one will be injected in the mojo under test:
       
https://github.com/apache/maven-install-plugin/blob/master/src/main/java/org/apache/maven/plugins/install/InstallMojo.java#L55
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to