You are viewing a plain text version of this content. The canonical link for it is here.
Posted to site-commits@maven.apache.org by sv...@apache.org on 2024/12/07 13:04:22 UTC
svn commit: r1922361 - /maven/website/content/plugin-developers/plugin-testing.html
Author: svn-site-role
Date: Sat Dec 7 13:04:21 2024
New Revision: 1922361
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 (original)
+++ maven/website/content/plugin-developers/plugin-testing.html Sat Dec 7 13:04:21 2024
@@ -149,14 +149,14 @@
<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, most mojos need more information to work properly. For example, you'll probably need to inject a reference to a MavenProject, so your mojo can query project variables.</p></section><section><a id="Using_PlexusTestCase"></a>
+<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>
<h2>Using PlexusTestCase</h2>
-<p>Mojo variables are injected using Plexus, and many Mojos are written to take specific advantage of the Plexus container (by executing a lifecycle or having various injected dependencies).</p>
-<p>If all you need are Plexus container services, you can write your class with <code>extends PlexusTestCase</code> instead of TestCase.</p>
+<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 <b>lookup()</b>> 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>
<p>With that said, if you need to inject Maven objects into your mojo, you'll probably prefer to use the maven-plugin-testing-harness.</p></section><section><a id="maven-plugin-testing-harness"></a>
<h2>maven-plugin-testing-harness</h2>
<p>The <a href="/plugin-testing/maven-plugin-testing-harness/">maven-plugin-testing-harness</a> is explicitly intended to test the <code>org.apache.maven.reporting.AbstractMavenReport#execute()</code> implementation.</p>
-<p>In general, you need to include <code>maven-plugin-testing-harness</code> as a dependency, and create a *MojoTest (by convention) class which <code>extends AbstractMojoTestCase</code>.</p>
+<p>In general, you need to include <code>maven-plugin-testing-harness</code> as a test-scoped dependency, and create a MojoTest (by convention) class which <code>extends AbstractMojoTestCase</code>.</p>
<pre class="prettyprint linenums"><code>...
<dependencies>
...
@@ -194,15 +194,15 @@
assertNotNull( mojo );
}
}</code></pre>
-<p>For more information, refer to <a class="externalLink" href="http://cwiki.apache.org/confluence/display/MAVENOLD/Maven+Plugin+Harness">Maven Plugin Harness Wiki</a></p></section></section><section><a id="Integration.2FFunctional_testing"></a>
+<p>For more information, refer to <a class="externalLink" href="https://cwiki.apache.org/confluence/display/MAVENOLD/Maven+Plugin+Harness">Maven Plugin Harness Wiki</a></p></section></section><section><a id="Integration.2FFunctional_testing"></a>
<h1>Integration/Functional testing</h1><section><a id="maven-verifier"></a>
<h2>maven-verifier</h2>
-<p>maven-verifier tests are run using JUnit or TestNG, and provide a simple class allowing you to launch Maven and assert on its log file and built artifacts. It also provides a ResourceExtractor, which extracts a Maven project from your src/test/resources directory into a temporary working directory where you can do tricky stuff with it. Follow the <a href="/shared/maven-verifier/getting-started.html">Getting Started</a> guide to learn more about creating maven-verifier tests.</p>
-<p>Maven itself uses maven-verifier to run its core integration tests. For more information, please refer to <a class="externalLink" href="https://cwiki.apache.org/confluence/display/MAVEN/Creating+a+Maven+Integration+Test">Creating a Maven Integration Test</a>.</p>
+<p>maven-verifier tests are run using JUnit, and provide a simple class allowing you to launch Maven and assert on its log file and built artifacts. It also provides a <code>ResourceExtractor</code>, which extracts a Maven project from the src/test/resources directory into a temporary working directory where you can do tricky stuff with it. Follow the <a href="/shared/maven-verifier/getting-started.html">Getting Started</a> guide to learn more about creating maven-verifier tests.</p>
+<p>Maven itself uses maven-verifier to run its core integration tests. For more information, see <a class="externalLink" href="https://cwiki.apache.org/confluence/display/MAVEN/Creating+a+Maven+Integration+Test">Creating a Maven Integration Test</a>.</p>
<p><b>Note</b>: maven-verifier and maven-verifier-plugin sound similar, but are totally different unrelated pieces of code. maven-verifier-plugin simply verifies the existence/absence of files on the filesystem. You could use it for functional testing, but you may need more features than maven-verifier-plugin provides.</p></section><section><a id="maven-invoker-plugin"></a>
<h2>maven-invoker-plugin</h2>
<p>You can use <a class="externalLink" href="https://maven.apache.org/plugins/maven-invoker-plugin/">maven-invoker-plugin</a> to invoke Maven and to provide some BeanShell/Groovy tests. Tests written in this way don't run under JUnit/TestNG; instead, they're run by Maven itself.</p>
-<p>You can take a look at the <a class="externalLink" href="https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-install-plugin/src/it/">maven-install-plugin</a> how there are integration tests are written.</p>
+<p>You can take a look at the <a class="externalLink" href="https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-install-plugin/src/it/">maven-install-plugin</a> to see how integration tests are written.</p>
<pre class="prettyprint linenums"><code><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...