Skip to content

JUnit field injection#284

Merged
prdoyle merged 13 commits intoboskworks:mainfrom
prdoyle:class-injection
Mar 29, 2026
Merged

JUnit field injection#284
prdoyle merged 13 commits intoboskworks:mainfrom
prdoyle:class-injection

Conversation

@prdoyle
Copy link
Copy Markdown
Collaborator

@prdoyle prdoyle commented Mar 29, 2026

JUnit's parameterization is not composable. Any given class has to decide, once and for all, what all its argument sources are, and that means (for example) superclasses can't introduce their own parameters and argument sources.

This PR extends our existing injection extension so it can inject into fields annotated with @Injected.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the bosk-junit injection mechanism to support class-level field injection (via @Injected fields and a new @InjectFields class template), enabling more composable test parameterization than JUnit’s @ParameterizedClass.

Changes:

  • Introduces Injector (replacing ParameterInjector) with unified support for parameters and fields (AnnotatedElement-based).
  • Adds field injection support (@Injected, @InjectFields, FieldInjectionContextProvider) and shared branching logic (ParameterInjectionSupport).
  • Refactors multiple test suites (boson, mongo, sql, testing, core) to use @InjectFrom + field injection instead of JUnit @ParameterizedClass, and adds new injectors/docs.

Reviewed changes

Copilot reviewed 46 out of 46 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
boson/src/test/java/works/bosk/boson/types/DataTypeHasWildcardsTest.java Migrates test injectors to the new Injector API.
boson/src/test/java/works/bosk/boson/codec/io/JsonSuiteTest.java Replaces @ParameterizedClass usage with @InjectFrom.
boson/src/test/java/works/bosk/boson/codec/io/JsonReaderValidateSyntaxTest.java Switches from constructor/class parameterization to field injection + standard @Test lifecycle.
boson/src/test/java/works/bosk/boson/codec/io/JsonReaderValidateSyntaxInjector.java Adds dedicated injector supplying validate-syntax reader factories.
boson/src/test/java/works/bosk/boson/codec/io/JsonReaderInvalidTokenTest.java Replaces @ParameterizedClass usage with @InjectFrom.
boson/src/test/java/works/bosk/boson/codec/io/JsonReaderInjector.java Adds shared JsonReader supplier injector for reader-based test suites.
boson/src/test/java/works/bosk/boson/codec/io/JsonReaderHappyTest.java Replaces @ParameterizedClass usage with @InjectFrom.
boson/src/test/java/works/bosk/boson/codec/io/AbstractJsonReaderTest.java Converts class-level parameterization to injected field (Function<String, JsonReader>).
boson/src/test/java/works/bosk/boson/codec/SettingsInjector.java Updates settings injector to Injector API.
boson/src/test/java/works/bosk/boson/codec/RoundTripTest.java Migrates from constructor parameterization to @InjectFields + injected settings.
boson/src/test/java/works/bosk/boson/codec/PrimitiveTypeInjector.java Updates primitive type injector to Injector API.
boson/src/test/java/works/bosk/boson/codec/PrimitiveInjector.java Updates primitive-number injector to Injector API.
boson/src/test/java/works/bosk/boson/codec/CodecHappyParseTest.java Converts settings from ctor parameter to injected field + @BeforeEach init.
boson/src/test/java/works/bosk/boson/codec/BasicCodecTest.java Converts settings from ctor parameter to injected field + @BeforeEach init.
boson/build.gradle Adds bosk-junit as a test dependency for boson tests.
bosk-testing/src/test/java/works/bosk/testing/drivers/ForgottenFlushTest.java Updates initial-state method references to instance form.
bosk-testing/src/main/java/works/bosk/testing/drivers/SharedDriverConformanceTest.java Updates initial-state method references to instance form.
bosk-testing/src/main/java/works/bosk/testing/drivers/DriverConformanceTest.java Migrates internal injectors to Injector API and AnnotatedElement checks.
bosk-testing/src/main/java/works/bosk/testing/drivers/AbstractDriverTest.java Makes initialState an instance method; adjusts logging to keep TestInfo in a field.
bosk-sql/src/test/java/works/bosk/drivers/sql/SqlDriverDurabilityTest.java Replaces @ParameterizedClass with field injection for database selection.
bosk-sql/src/test/java/works/bosk/drivers/sql/DatabaseInjector.java Adds database injector for SQL driver tests.
bosk-mongo/src/test/java/works/bosk/drivers/mongo/internal/To.java Adds field marker annotation to disambiguate injected configs.
bosk-mongo/src/test/java/works/bosk/drivers/mongo/internal/SchemaEvolutionTest.java Replaces class parameterization with field injection + marker annotations for cartesian config pairs.
bosk-mongo/src/test/java/works/bosk/drivers/mongo/internal/MongoDriverHanoiTest.java Replaces @ParameterizedClass with field injection and @BeforeEach setup.
bosk-mongo/src/test/java/works/bosk/drivers/mongo/internal/From.java Adds field marker annotation to disambiguate injected configs.
bosk-mongo/src/test/java/works/bosk/drivers/mongo/internal/DottedFieldNameTest.java Extends AbstractDriverTest and migrates initial-state method reference usage.
bosk-mongo/build.gradle Adds bosk-junit as a test dependency for mongo tests.
bosk-junit/src/test/java/works/bosk/junit/ParameterizedClassTest.java Removes old tests tied to @ParameterizedClass/ParameterInjector.
bosk-junit/src/test/java/works/bosk/junit/ParameterInjectionContextProviderTest.java Removes old tests for the prior parameter injection implementation.
bosk-junit/src/test/java/works/bosk/junit/CorrelatedParameterTest.java Removes manual/disabled correlated injection test for old API.
bosk-junit/src/test/java/works/bosk/junit/InjectFromHappyPathTests.java Adds new happy-path tests for field and method injection behavior under JUnit.
bosk-junit/src/main/java/works/bosk/junit/ParameterInjectionSupport.java Introduces shared branch/cartesian-product/injector instantiation logic used by both injection modes.
bosk-junit/src/main/java/works/bosk/junit/ParameterInjectionContextProvider.java Refactors to use shared support and incorporate class-level branch state.
bosk-junit/src/main/java/works/bosk/junit/Injector.java Renames/expands injector interface to support both fields and parameters.
bosk-junit/src/main/java/works/bosk/junit/InjectedTest.java Updates documentation to reflect Injector + field injection options.
bosk-junit/src/main/java/works/bosk/junit/Injected.java Adds new annotation for field injection.
bosk-junit/src/main/java/works/bosk/junit/InjectFrom.java Updates annotation to reference Injector and include fields in scope.
bosk-junit/src/main/java/works/bosk/junit/InjectFields.java Adds new class-level annotation enabling field injection via @ClassTemplate.
bosk-junit/src/main/java/works/bosk/junit/FieldInjectionContextProvider.java Adds new class template provider that instantiates test classes and populates injected fields.
bosk-junit/src/main/java/module-info.java Adds SLF4J module requirement for new logging.
bosk-junit/USERS.md Adds a user guide for injection features and usage patterns.
bosk-core/src/test/java/works/bosk/drivers/ReplicaSetTest.java Updates initial-state method references to instance form.
bosk-core/src/test/java/works/bosk/drivers/ReplicaSetConformanceTest.java Updates initial-state method references to instance form.
bosk-core/src/test/java/works/bosk/BoskContextTest.java Updates initial-state method references to instance form.
bosk-core/src/test/java/works/bosk/IdentifierTest.java Migrates injectors to Injector API and AnnotatedElement checks.
CLAUDE.md Adds commit hygiene guidelines to contributor guidance.
Comments suppressed due to low confidence (1)

bosk-junit/src/main/java/works/bosk/junit/Injector.java:44

  • Injector.ofType will never match anything: it compares element (a Parameter/Field) to type (a Class). This makes the helper unusable and silently breaks injection when callers use Injector.ofType. Compare elementType (or ((Parameter)element).getType()) to type instead.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@prdoyle prdoyle enabled auto-merge March 29, 2026 21:31
@prdoyle prdoyle disabled auto-merge March 29, 2026 21:32
@prdoyle prdoyle enabled auto-merge March 29, 2026 21:34
@prdoyle prdoyle merged commit 8bf0e05 into boskworks:main Mar 29, 2026
1 check passed
@prdoyle prdoyle deleted the class-injection branch March 29, 2026 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants