Class AnnotatedField<A extends Annotation,T>
java.lang.Object
ru.vyarus.dropwizard.guice.test.jupiter.env.field.AnnotatedField<A,T>
- Type Parameters:
A- annotation typeT- value type
Annotated field wrapper. Used to simplify work with test fields by hiding all required reflection.
- Since:
- 07.02.2025
-
Constructor Summary
ConstructorsConstructorDescriptionAnnotatedField(A annotation, Field field, Class<?> testClass) Create annotated field for exact field and annotation. -
Method Summary
Modifier and TypeMethodDescriptioncheckValueNotChanged(org.junit.jupiter.api.extension.TestInstances instance) Validates that field value not changed (it is the same value as was injected).voidClear custom state.findRequiredInstance(org.junit.jupiter.api.extension.TestInstances instances) In case of nested tests, test instances would contain multiple test instances.Use this method to avoid duplicate value readings when you're SURE that value was already resolved.<K> KgetCustomData(String key) Get custom value.Class<?>getField()getName()getType()For example if the field type is:RootType<Param1, Param2>.getValue(org.junit.jupiter.api.extension.TestInstances instances) In case of nested test, there would be root class instance and nested instance.booleanisCompatible(Object instance) Required to prevent incorrect usage (field resolution with a wrong instance).booleanisCustomDataSet(String key) Note: if key set with null value - it would be considered as false.booleanisStatic()booleanvoidValidation option: throw error if field is static.voidValidation option: throw error if field is not static.voidsetCustomData(String key, Object value) Apply custom value for the field object.voidvoidIn case of nested test, there would be root class instance and nested instance.toString()
-
Constructor Details
-
AnnotatedField
Create annotated field for exact field and annotation.- Parameters:
annotation- field annotationfield- annotated fieldtestClass- test class where field was searcher
-
-
Method Details
-
getAnnotation
- Returns:
- field annotation instance (type of annotation defined by initial fields search)
-
getType
- Returns:
- field class type
-
getTypeParameters
For example if the field type is:RootType<Param1, Param2>. Then the method would return [Param1, Param2].Implementation does not expect not resolved variables (simple case).
- Returns:
- type arguments
-
getDeclaringClass
- Returns:
- class that declares field
-
getName
- Returns:
- field name
-
getField
- Returns:
- field instance
-
getValue
- Parameters:
instance- test instance (or null for static field)- Returns:
- test field value
- Throws:
IllegalStateException- if non-static field resolved with null instance or other error appear
-
getValue
In case of nested test, there would be root class instance and nested instance. It is important to select the correct instance for field manipulation: the correct test instance would be selected by preserved test class.- Parameters:
instances- test instances- Returns:
- field value
-
setValue
- Parameters:
instance- test instance (or null for static field)value- field value- Throws:
IllegalStateException- if non-static field set with null instance or other error appear
-
setValue
In case of nested test, there would be root class instance and nested instance. It is important to select the correct instance for field manipulation: the correct test instance would be selected by preserved test class.- Parameters:
instances- test instancesvalue- value to set
-
isStatic
public boolean isStatic()- Returns:
- true if field is static
-
isTestOwnField
public boolean isTestOwnField()- Returns:
- true if field declared directly in test class (not in base class)
-
requireStatic
public void requireStatic()Validation option: throw error if field is not static. -
requireNonStatic
public void requireNonStatic()Validation option: throw error if field is static. -
getCachedValue
Use this method to avoid duplicate value readings when you're SURE that value was already resolved.- Returns:
- last obtained or set value
-
isCompatible
Required to prevent incorrect usage (field resolution with a wrong instance).- Parameters:
instance- instance to check- Returns:
- true if the provided instance is a field class instance, false otherwise
-
findRequiredInstance
In case of nested tests, test instances would contain multiple test instances. It is important to select the correct one (using preserved original test class).- Parameters:
instances- test instances- Returns:
- test instance or null
- Throws:
IllegalStateException- if a test instances object provided but does not contain the required test instance
-
setCustomData
Apply custom value for the field object. Useful during field processing to mark is as processed or assign an additional state.- Parameters:
key- keyvalue- value
-
getCustomData
Get custom value.- Type Parameters:
K- value type- Parameters:
key- key- Returns:
- value or null
-
isCustomDataSet
Note: if key set with null value - it would be considered as false.- Parameters:
key- key- Returns:
- true if non null custom value set
-
clearCustomData
public void clearCustomData()Clear custom state. -
toStringField
- Returns:
- to string field with class and reduced package
-
checkValueNotChanged
Validates that field value not changed (it is the same value as was injected).- Parameters:
instance- test instance- Returns:
- value
- Throws:
IllegalStateException- if field value differs from the injected value
-
toString
-