This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-site.git
The following commit(s) were added to refs/heads/master by this push:
new 69775263 Replace PlexusTestCase with plexus-testing's @PlexusTest in
the plugin testing guide
69775263 is described below
commit 69775263c557b7eb973dcab51af045b57065a4e5
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Mon Nov 2 08:28:01 2026 +0000
Replace PlexusTestCase with plexus-testing's @PlexusTest in the plugin
testing guide
PlexusTestCase is a JUnit 3 base class. The old heading's anchor is kept.
The junit-bom import and the Sisu index are both needed: without the
first, plexus-testing's newer JUnit API clashes with the engine; without
the second, the container finds no @Named component.
---
.../markdown/plugin-developers/plugin-testing.md | 86 +++++++++++++++++++++-
1 file changed, 84 insertions(+), 2 deletions(-)
diff --git a/content/markdown/plugin-developers/plugin-testing.md
b/content/markdown/plugin-developers/plugin-testing.md
index d94eab0e..a974821e 100644
--- a/content/markdown/plugin-developers/plugin-testing.md
+++ b/content/markdown/plugin-developers/plugin-testing.md
@@ -82,11 +82,93 @@ class GreetingMojoTest {
}
```
-## Using PlexusTestCase
+<a id="Using_PlexusTestCase"></a>
+
+## Using plexus-testing
Mojo variables are injected by Guice, sometimes with a Plexus adapter to
support the legacy `@Component` annotation. Currently some mojos are fully
guicified with constructor injection, while others that have not yet been
converted use Plexus field injection.
-Both Guice-based and Plexus-based mojos rely on the Guice Plexus adapter to
inject dependencies by having the test class extend `PlexusTestCase` and
calling the **lookup()**> method to instantiate the mojo. Tests for fully
Guicified mojos can also inject dependencies directly into the constructor
without extending `PlexusTestCase`. 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 o [...]
+Tests for fully Guicified mojos can inject dependencies directly into the
constructor. 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.
+
+To test the components a mojo uses together with their real dependencies,
annotate the test class with `@PlexusTest` from
[plexus-testing](https://github.com/codehaus-plexus/plexus-testing). It starts
a Plexus container for each test, injects the test class's `@Inject` fields,
and lets you look components up from the injected `PlexusContainer`. It
replaces the JUnit 3 base class `PlexusTestCase`.
+
+```xml
+<dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.junit</groupId>
+ <artifactId>junit-bom</artifactId>
+ <version>5.11.0</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+</dependencyManagement>
+
+<dependencies>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-testing</artifactId>
+ <version>2.2.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
+</dependencies>
+
+<build>
+ <plugins>
+ <plugin>
+ <groupId>org.eclipse.sisu</groupId>
+ <artifactId>sisu-maven-plugin</artifactId>
+ <version>1.1.0</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>main-index</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+</build>
+```
+
+The `junit-bom` import keeps the JUnit version that plexus-testing brings in
line with your other JUnit dependencies; without it, mixed versions fail with a
`NoSuchMethodError`. The container finds `@Named` components through the index
in `META-INF/sisu/javax.inject.Named`, which the `main-index` goal of
sisu-maven-plugin writes. A plugin that inherits from the Maven parent POM gets
the `junit-bom` import and the plugin's configuration from its parent, and only
needs to add `sisu-maven-p [...]
+
+For a component such as the following (its annotations come from
`javax.inject`, which a plugin usually declares with `provided` scope)
+
+```java
+@Named
+@Singleton
+public class Greeter {
+ public String greet(String name) {
+ return "Hello " + name;
+ }
+}
+```
+
+the test looks like this:
+
+```java
+@PlexusTest
+class GreeterTest {
+ @Inject
+ private Greeter greeter;
+
+ @Inject
+ private PlexusContainer container;
+
+ @Test
+ void injectsAndLooksUp() throws Exception {
+ assertEquals("Hello Maven", greeter.greet("Maven"));
+ assertEquals("Hello lookup",
container.lookup(Greeter.class).greet("lookup"));
+ }
+}
+```
With that said, if you need to inject Maven objects into your mojo, you'll
probably prefer to use the maven-plugin-testing-harness.