@@ -151,6 +151,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
151151 binder .install (new MavenProvidesModule (context .getRequiredTestInstance ()));
152152 binder .requestInjection (context .getRequiredTestInstance ());
153153 binder .bind (Log .class ).toInstance (new MojoLogWrapper (LoggerFactory .getLogger ("anonymous" )));
154+ binder .bind (MavenProject .class ).toInstance (mockMavenProject ());
154155 binder .bind (MavenSession .class ).toInstance (mockMavenSession ());
155156 binder .bind (MojoExecution .class ).toInstance (mockMojoExecution ());
156157 });
@@ -180,9 +181,7 @@ public void beforeEach(ExtensionContext context) throws Exception {
180181 * @return a MojoExecution mock
181182 */
182183 private MojoExecution mockMojoExecution () {
183- MojoExecution mockExecution = Mockito .mock (MojoExecution .class );
184- lenient ().when (mockExecution .getMojoDescriptor ()).thenReturn (new MojoDescriptor ());
185- return mockExecution ;
184+ return Mockito .mock (MojoExecution .class );
186185 }
187186
188187 /**
@@ -197,6 +196,17 @@ private MavenSession mockMavenSession() {
197196 return session ;
198197 }
199198
199+ /**
200+ * Default MavenProject mock
201+ *
202+ * @return a MavenProject mock
203+ */
204+ private MavenProject mockMavenProject () {
205+ MavenProject mavenProject = Mockito .mock (MavenProject .class );
206+ lenient ().when (mavenProject .getProperties ()).thenReturn (new Properties ());
207+ return mavenProject ;
208+ }
209+
200210 protected String getPluginDescriptorLocation () {
201211 return "META-INF/maven/plugin.xml" ;
202212 }
@@ -266,27 +276,32 @@ protected Mojo lookupMojo(
266276 PlexusContainer plexusContainer = getContainer (extensionContext );
267277 // pluginkey = groupId : artifactId : version : goal
268278 Mojo mojo = plexusContainer .lookup (Mojo .class , coord [0 ] + ":" + coord [1 ] + ":" + coord [2 ] + ":" + coord [3 ]);
269- for (MojoDescriptor mojoDescriptor : descriptor .getMojos ()) {
270- if (Objects .equals (
271- mojoDescriptor .getImplementation (), mojo .getClass ().getName ())) {
272- if (pluginConfiguration != null ) {
273- pluginConfiguration = finalizeConfig (pluginConfiguration , mojoDescriptor );
274- }
275- }
279+
280+ Optional <MojoDescriptor > mojoDescriptor = descriptor .getMojos ().stream ()
281+ .filter (md -> Objects .equals (md .getImplementation (), mojo .getClass ().getName ()))
282+ .findFirst ();
283+
284+ if (mojoDescriptor .isPresent ()) {
285+ pluginConfiguration = finalizeConfig (pluginConfiguration , mojoDescriptor .get ());
286+ }
287+
288+ MavenSession session = plexusContainer .lookup (MavenSession .class );
289+ MavenProject mavenProject = plexusContainer .lookup (MavenProject .class );
290+ MojoExecution mojoExecution = plexusContainer .lookup (MojoExecution .class );
291+
292+ if (Mockito .mockingDetails (session ).isMock ()) {
293+ lenient ().when (session .getCurrentProject ()).thenReturn (mavenProject );
294+ }
295+
296+ if (Mockito .mockingDetails (mavenProject ).isMock ()) {
297+ lenient ().when (mavenProject .getBasedir ()).thenReturn (new File (getTestBasedir (extensionContext )));
298+ }
299+
300+ if (mojoDescriptor .isPresent () && Mockito .mockingDetails (mojoExecution ).isMock ()) {
301+ lenient ().when (mojoExecution .getMojoDescriptor ()).thenReturn (mojoDescriptor .get ());
276302 }
303+
277304 if (pluginConfiguration != null ) {
278- MavenSession session = plexusContainer .lookup (MavenSession .class );
279- try {
280- plexusContainer .lookup (MavenProject .class );
281- } catch (ComponentLookupException ignore ) {
282- // nothing
283- }
284- MojoExecution mojoExecution ;
285- try {
286- mojoExecution = plexusContainer .lookup (MojoExecution .class );
287- } catch (ComponentLookupException e ) {
288- mojoExecution = null ;
289- }
290305 ExpressionEvaluator evaluator =
291306 new WrapEvaluator (plexusContainer , new PluginParameterExpressionEvaluator (session , mojoExecution ));
292307 ComponentConfigurator configurator = new BasicComponentConfigurator ();
@@ -299,6 +314,19 @@ protected Mojo lookupMojo(
299314
300315 mojo .setLog (plexusContainer .lookup (Log .class ));
301316
317+ // clear invocations on mocks to avoid test interference
318+ if (Mockito .mockingDetails (session ).isMock ()) {
319+ Mockito .clearInvocations (session );
320+ }
321+
322+ if (Mockito .mockingDetails (mavenProject ).isMock ()) {
323+ Mockito .clearInvocations (mavenProject );
324+ }
325+
326+ if (Mockito .mockingDetails (mojoExecution ).isMock ()) {
327+ Mockito .clearInvocations (mojoExecution );
328+ }
329+
302330 return mojo ;
303331 }
304332
@@ -351,19 +379,6 @@ public static Xpp3Dom extractPluginConfiguration(String artifactId, Xpp3Dom pomD
351379 return pluginConfigurationElement ;
352380 }
353381
354- /**
355- * sometimes the parent element might contain the correct value so generalize that access
356- *
357- * TODO find out where this is probably done elsewhere
358- */
359- private static String resolveFromRootThenParent (Xpp3Dom pluginPomDom , String element ) throws Exception {
360- return Optional .ofNullable (child (pluginPomDom , element ).orElseGet (() -> child (pluginPomDom , "parent" )
361- .flatMap (e -> child (e , element ))
362- .orElse (null )))
363- .map (Xpp3Dom ::getValue )
364- .orElseThrow (() -> new Exception ("unable to determine " + element ));
365- }
366-
367382 /**
368383 * Convenience method to obtain the value of a variable on a mojo that might not have a getter.
369384 * <br>
@@ -425,7 +440,7 @@ public static void setVariableValueToObject(Object object, String variable, Obje
425440 field .set (object , value );
426441 }
427442
428- static class WrapEvaluator implements TypeAwareExpressionEvaluator {
443+ private static class WrapEvaluator implements TypeAwareExpressionEvaluator {
429444
430445 private final PlexusContainer container ;
431446
@@ -473,6 +488,10 @@ public File alignToBaseDirectory(File path) {
473488 private static class MavenProvidesModule implements Module {
474489 private final Object testInstance ;
475490
491+ MavenProvidesModule (Object testInstance ) {
492+ this .testInstance = testInstance ;
493+ }
494+
476495 @ Override
477496 @ SuppressWarnings ("unchecked" )
478497 public void configure (Binder binder ) {
@@ -486,15 +505,14 @@ public void configure(Binder binder) {
486505 try {
487506 method .setAccessible (true );
488507 Object value = method .invoke (testInstance );
508+ if (value == null ) {
509+ throw new IllegalArgumentException ("Provides method returned null: " + method );
510+ }
489511 binder .bind ((Class <Object >) method .getReturnType ()).toInstance (value );
490512 } catch (IllegalAccessException | InvocationTargetException e ) {
491- throw new IllegalStateException (e );
513+ throw new IllegalArgumentException (e );
492514 }
493515 }
494516 }
495-
496- MavenProvidesModule (Object testInstance ) {
497- this .testInstance = testInstance ;
498- }
499517 }
500518}
0 commit comments