This is an automated email from the ASF dual-hosted git repository.
veithen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-axiom.git
The following commit(s) were added to refs/heads/master by this push:
new 19a1b0d4b Document constructor injection as alternative in migration
guide
19a1b0d4b is described below
commit 19a1b0d4be1e0c62488987ac5bcb66ebe7019162
Author: Andreas Veithen-Knowles <[email protected]>
AuthorDate: Sat Mar 21 11:51:19 2026 +0000
Document constructor injection as alternative in migration guide
Add a 'Constructor injection as an alternative to field injection' section
to the Tips and lessons learned. This covers cases where the base test case
class stores dependencies in final fields set by the constructor, making
field injection impractical. Examples are drawn from the blob module
(WritableBlobTestCase hierarchy).
---
testing/matrix-testsuite/migration.md | 58 +++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git a/testing/matrix-testsuite/migration.md
b/testing/matrix-testsuite/migration.md
index 0585e22c0..598409c56 100644
--- a/testing/matrix-testsuite/migration.md
+++ b/testing/matrix-testsuite/migration.md
@@ -433,6 +433,64 @@ the same type is added later.
See the `dom-testsuite` module for an example with two boolean dimensions
(`"deep"` and `"newChildHasSiblings"`).
+### Constructor injection as an alternative to field injection
+
+The examples above use `@Inject` on fields, but in some cases it is necessary
+or more convenient to use **constructor injection** instead. This is the case
+when the base test case class stores dependencies in `final` fields set by
+the constructor. Converting those fields to non-final `@Inject` fields would
+require changing how the class and its subclasses are structured, which may
+not be desirable.
+
+With constructor injection, the existing constructor chain is preserved:
subclass
+constructors receive dependencies as parameters and pass them up via
`super(…)`.
+To make this work with Guice, simply add `@Inject` to the subclass constructor
+(there is no need to annotate the base class constructor). Guice will then
inject
+the parameters when creating the test instance.
+
+**Base test case class** (no `@Inject` needed here — constructor stays as-is):
+
+```java
+public abstract class WritableBlobTestCase extends TestCase {
+ private final WritableBlobFactory<?> factory;
+ private final State initialState;
+
+ public WritableBlobTestCase(WritableBlobFactory<?> factory, State
initialState) {
+ this.factory = factory;
+ this.initialState = initialState;
+ }
+ // ...
+}
+```
+
+**Simple subclass** — add `@Inject` to the constructor:
+
+```java
+public class TestAvailable extends WritableBlobTestCase {
+ @Inject
+ public TestAvailable(WritableBlobFactory<?> factory) {
+ super(factory, State.NEW);
+ }
+ // ...
+}
+```
+
+**Subclass with `@Named` parameter** — use `@Named` on the constructor
+parameter just as you would on a field:
+
+```java
+public class TestMarkReset extends SizeSensitiveWritableBlobTestCase {
+ @Inject
+ public TestMarkReset(WritableBlobFactory<?> factory, @Named("size") int
size) {
+ super(factory, State.NEW, size);
+ }
+ // ...
+}
+```
+
+See the `blob` module (`WritableBlobTestCase` hierarchy) for a complete
+example.
+
## Checklist
### Reusable API test suites