Author: svn-site-role
Date: Mon Oct 20 21:26:13 2025
New Revision: 1929244
Log:
Site checkin for project Apache Maven Site
Modified:
maven/website/content/plugin-developers/plugin-testing.html
Modified: maven/website/content/plugin-developers/plugin-testing.html
==============================================================================
--- maven/website/content/plugin-developers/plugin-testing.html Mon Oct 20
20:47:38 2025 (r1929243)
+++ maven/website/content/plugin-developers/plugin-testing.html Mon Oct 20
21:26:13 2025 (r1929244)
@@ -176,8 +176,47 @@ under the License.
<p>The general wisdom is that your code should be mostly tested with unit
tests, but should also have some functional tests.</p></section><section><a
id="Unit_Tests"></a>
<h1>Unit Tests</h1><section><a id="Using_JUnit_alone"></a>
<h2>Using JUnit alone</h2>
-<p>In principle, you can write a unit test of a plugin Mojo the same way you'd
write any other JUnit test case, by writing a class that <code>extends
TestCase</code>.</p>
-<p>However, many mojo methods need more information to work properly. For
example, you'll probably need to inject a reference to a
<code>MavenProject</code>, so your mojo can query project
variables.</p></section><section><a id="Using_PlexusTestCase"></a>
+<p>In principle, you can write a unit test of a plugin Mojo the same way you'd
write any other JUnit test case.</p>
+<p>When using injections in your Mojo, you can simply use a mocking framework
such as Mockito to create mock instances of the injected dependencies,
+and pass them to the Mojo constructor (if using constructor injection) or set
them on the Mojo instance (if using field injection).</p>
+<p>Simple example using Mockito with constructor injection:</p>
+
+<pre class="prettyprint linenums"><code class="language-java">@Mojo(name =
"sayhi")
+public class GreetingMojo extends AbstractMojo {
+
+ private final MavenProject project;
+
+ @Inject
+ public GreetingMojo(MavenProject project) {
+ this.project = project;
+ }
+
+ @Override
+ public void execute() throws MojoExecutionException {
+ getLog().info("Hello, world.");
+ }
+}
+</code></pre>
+<p>and the corresponding unit test:</p>
+
+<pre class="prettyprint linenums"><code
class="language-java">@ExtendWith(MockitoExtension.class)
+class GreetingMojoTest {
+ @Mock
+ private MavenProject mavenProject;
+
+ @InjectMocks
+ private GreetingMojo mojo;
+
+ @Test
+ void testExecute() throws MojoExecutionException {
+ // Execute the Mojo
+ mojo.execute();
+
+ // Verify behavior or state as needed
+ // (e.g., check interactions with mock, etc.)
+ }
+}
+</code></pre></section><section><a id="Using_PlexusTestCase"></a>
<h2>Using PlexusTestCase</h2>
<p>Mojo variables are injected by Guice, sometimes with a Plexus adapter to
support the legacy <code>@Component</code> annotation. Currently some mojos are
fully guicified with constructor injection, while others that have not yet been
converted use Plexus field injection.</p>
<p>Both Guice-based and Plexus-based mojos rely on the Guice Plexus adapter to
inject dependencies by having the test class extend <code>PlexusTestCase</code>
and calling the <strong>lookup()</strong>> method to instantiate the mojo.
Tests for fully Guicified mojos can also inject dependencies directly into the
constructor without extending <code>PlexusTestCase</code>. These dependencies
can be Mockito mocks or instances of the actual model classes. If a particular
test does not access the injected field — that is, it's only injected to
fulfill the constructor signature — you can usually also pass null as
the value of that argument.</p>