Class AnnotationBasedInjectingExtension<A extends Annotation>
- java.lang.Object
-
- com.github.robtimus.junit.support.extension.InjectingExtension
-
- com.github.robtimus.junit.support.extension.AnnotationBasedInjectingExtension<A>
-
- Type Parameters:
A
- The type of annotation to use for fields and/or parameters.
- All Implemented Interfaces:
BeforeAllCallback
,BeforeEachCallback
,Extension
,ParameterResolver
public abstract class AnnotationBasedInjectingExtension<A extends Annotation> extends InjectingExtension
An abstract base class for JUnit extensions that can inject values in fields and/or parameters, based on a specific annotation.A compatible annotation should look like this, where
MyExtension
extendsAnnotationBasedInjectingExtension<MyAnnotation>
:@ExtendWith(MyExtension.class) @Target({ ElementType.FIELD, ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { // add fields as needed }
- Author:
- Rob Spoor
- Since:
- 2.0
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AnnotationBasedInjectingExtension(Class<A> annotationType, MethodHandles.Lookup lookup)
Creates a new extension.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract Object
resolveValue(InjectionTarget target, A annotation, ExtensionContext context)
Resolves the value to inject.protected Object
resolveValue(InjectionTarget target, ExtensionContext context)
Resolves the value to inject.protected abstract Optional<JUnitException>
validateTarget(InjectionTarget target, A annotation, ExtensionContext context)
Validates that a target is valid for an injection annotation.protected Optional<JUnitException>
validateTarget(InjectionTarget target, ExtensionContext context)
Validates that a target is valid for injecting.-
Methods inherited from class com.github.robtimus.junit.support.extension.InjectingExtension
beforeAll, beforeEach, resolveParameter, supportsParameter
-
-
-
-
Constructor Detail
-
AnnotationBasedInjectingExtension
protected AnnotationBasedInjectingExtension(Class<A> annotationType, 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:
annotationType
- The annotation type to check for.lookup
- The object to use for looking up the objects that are necessary for setting field values.- Throws:
NullPointerException
- If the given annotation type or lookup isnull
.
-
-
Method Detail
-
validateTarget
protected final Optional<JUnitException> validateTarget(InjectionTarget target, ExtensionContext context)
Description copied from class:InjectingExtension
Validates that a target is valid for injecting.If this method returns a non-empty
Optional
for parameter injection,InjectingExtension.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.- Specified by:
validateTarget
in classInjectingExtension
- 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)
.
-
validateTarget
protected abstract Optional<JUnitException> validateTarget(InjectionTarget target, A annotation, ExtensionContext context)
Validates that a target is valid for an injection annotation.This method will be called from
validateTarget(InjectionTarget, ExtensionContext)
when the annotation is present on the injection target.For field injection, the field is found using only the annotation. If this method returns a non-empty
Optional
, the exception is thrown. This is used to indicate the field is not properly configured, e.g. because it has an incorrect type.- Parameters:
target
- The target to validate; nevernull
.annotation
- The injection annotation found on the target; nevernull
.context
- The current extension context; nevernull
.- Returns:
Optional.empty()
if the given target is valid for the given annotation, 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)
.
-
resolveValue
protected final Object resolveValue(InjectionTarget target, ExtensionContext context) throws Exception
Description copied from class:InjectingExtension
Resolves the value to inject.When this method is called for parameter injection,
InjectingExtension.supportsParameter(ParameterContext, ExtensionContext)
will have returnedtrue
, which means thatInjectingExtension.validateTarget(InjectionTarget, ExtensionContext)
will have returned an emptyOptional
.When this method is called for field injection,
InjectingExtension.validateTarget(InjectionTarget, ExtensionContext)
will have been called and verified to have returned an emptyOptional
.- Specified by:
resolveValue
in classInjectingExtension
- 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.
-
resolveValue
protected abstract Object resolveValue(InjectionTarget target, A annotation, ExtensionContext context) throws Exception
Resolves the value to inject.When this method is called for parameter injection,
InjectingExtension.supportsParameter(ParameterContext, ExtensionContext)
will have returnedtrue
, which means thatvalidateTarget(InjectionTarget, ExtensionContext)
, and by proxyvalidateTarget(InjectionTarget, Annotation, ExtensionContext)
, will have returned an emptyOptional
.When this method is called for field injection,
validateTarget(InjectionTarget, ExtensionContext)
, and by proxyvalidateTarget(InjectionTarget, Annotation, ExtensionContext)
, will have been called and verified to have returned an emptyOptional
.- Parameters:
target
- The target to inject the value in; nevernull
.annotation
- The injection annotation found on the target; nevernull
.context
- The current extension context; nevernull
.- Returns:
- The value to inject; possibly
null
. - Throws:
Exception
- If the value could not be resolved.
-
-