Class InjectingExtension
- java.lang.Object
-
- com.github.robtimus.junit.support.extension.InjectingExtension
-
- All Implemented Interfaces:
BeforeAllCallback
,BeforeEachCallback
,Extension
,ParameterResolver
- Direct Known Subclasses:
AnnotationBasedInjectingExtension
public abstract class InjectingExtension extends Object implements BeforeAllCallback, BeforeEachCallback, ParameterResolver
An abstract base class for JUnit extensions that can inject values in fields and/or parameters.- Author:
- Rob Spoor
- Since:
- 2.0
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
InjectingExtension(Predicate<Field> fieldPredicate, MethodHandles.Lookup lookup)
Creates a new extension.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
beforeAll(ExtensionContext context)
void
beforeEach(ExtensionContext context)
Object
resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
protected abstract Object
resolveValue(InjectionTarget target, ExtensionContext context)
Resolves the value to inject.boolean
supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
protected abstract Optional<JUnitException>
validateTarget(InjectionTarget target, ExtensionContext context)
Validates that a target is valid for injecting.
-
-
-
Constructor Detail
-
InjectingExtension
protected InjectingExtension(Predicate<Field> fieldPredicate, MethodHandles.Lookup lookup)
Creates a new extension.Concrete sub classes should call
MethodHandles.lookup()
and pass the result to this constructor. This prevents extensions piggy-backing on access granted to this class' module.- Parameters:
fieldPredicate
- A predicate that determines which fields are eligible for injection.lookup
- The object to use for looking up the objects that are necessary for setting field values.- Throws:
NullPointerException
- If the given predicate or lookup isnull
.
-
-
Method Detail
-
beforeAll
public final void beforeAll(ExtensionContext context) throws Exception
- Specified by:
beforeAll
in interfaceBeforeAllCallback
- Throws:
Exception
-
beforeEach
public final void beforeEach(ExtensionContext context) throws Exception
- Specified by:
beforeEach
in interfaceBeforeEachCallback
- Throws:
Exception
-
supportsParameter
public final boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
- Specified by:
supportsParameter
in interfaceParameterResolver
-
validateTarget
protected abstract Optional<JUnitException> validateTarget(InjectionTarget target, ExtensionContext context)
Validates that a target is valid for injecting.If this method returns a non-empty
Optional
for parameter injection,supportsParameter(ParameterContext, ExtensionContext)
will returnfalse
, and JUnit will fail if no other extension supports the parameter.If this method returns a non-empty
Optional
for field injection, the exception is thrown. This situation may or may not be prevented using the field predicate used to create this extension. In some cases the predicate may not test all aspects that are used to inject a value into fields; if that's the case, throwing an error may be an appropriate action.- Parameters:
target
- The target to validate; nevernull
.context
- The current extension context; nevernull
.- Returns:
Optional.empty()
if the given target is valid for injecting, or anOptional
describing an exception that indicates why the target is invalid otherwise. In that case, the exception should have been created usingInjectionTarget.createException(String)
orInjectionTarget.createException(String, Throwable)
.
-
resolveParameter
public final Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
- Specified by:
resolveParameter
in interfaceParameterResolver
-
resolveValue
protected abstract Object resolveValue(InjectionTarget target, ExtensionContext context) throws Exception
Resolves the value to inject.When this method is called for parameter injection,
supportsParameter(ParameterContext, ExtensionContext)
will have returnedtrue
, which means thatvalidateTarget(InjectionTarget, ExtensionContext)
will have returned an emptyOptional
.When this method is called for field injection,
validateTarget(InjectionTarget, ExtensionContext)
will have been called and verified to have returned an emptyOptional
.- Parameters:
target
- The target to inject the value in; nevernull
.context
- The current extension context; nevernull
.- Returns:
- The value to inject; possibly
null
. - Throws:
Exception
- If the value could not be resolved.
-
-