Class AnnotatedField<A extends java.lang.annotation.Annotation,T>
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.jupiter.env.field.AnnotatedField<A,T>
-
- Type Parameters:
A- annotation typeT- value type
public class AnnotatedField<A extends java.lang.annotation.Annotation,T> extends java.lang.ObjectAnnotated field wrapper. Used to simplify work with test fields by hiding all required reflection.- Since:
- 07.02.2025
-
-
Constructor Summary
Constructors Constructor Description AnnotatedField(A annotation, java.lang.reflect.Field field, java.lang.Class<?> testClass)Create annotated field for exact field and annotation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TcheckValueNotChanged(org.junit.jupiter.api.extension.TestInstances instance)Validates that field value not changed (it is the same value as was injected).voidclearCustomData()Clear custom state.java.lang.ObjectfindRequiredInstance(org.junit.jupiter.api.extension.TestInstances instances)In case of nested tests, test instances would contain multiple test instances.AgetAnnotation()java.lang.ObjectgetCachedValue()Use this method to avoid duplicate value readings when you're SURE that value was already resolved.<K> KgetCustomData(java.lang.String key)Get custom value.java.lang.Class<?>getDeclaringClass()java.lang.reflect.FieldgetField()java.lang.StringgetName()java.lang.Class<T>getType()java.util.List<java.lang.Class<?>>getTypeParameters()For example if the field type is:RootType<Param1, Param2>.TgetValue(java.lang.Object instance)TgetValue(org.junit.jupiter.api.extension.TestInstances instances)In case of nested test, there would be root class instance and nested instance.booleanisCompatible(java.lang.Object instance)Required to prevent incorrect usage (field resolution with a wrong instance).booleanisCustomDataSet(java.lang.String key)Note: if key set with null value - it would be considered as false.booleanisStatic()booleanisTestOwnField()voidrequireNonStatic()Validation option: throw error if field is static.voidrequireStatic()Validation option: throw error if field is not static.voidsetCustomData(java.lang.String key, java.lang.Object value)Apply custom value for the field object.voidsetValue(java.lang.Object instance, T value)voidsetValue(org.junit.jupiter.api.extension.TestInstances instances, T value)In case of nested test, there would be root class instance and nested instance.java.lang.StringtoString()java.lang.StringtoStringField()
-
-
-
Constructor Detail
-
AnnotatedField
public AnnotatedField(A annotation, java.lang.reflect.Field field, java.lang.Class<?> testClass)
Create annotated field for exact field and annotation.- Parameters:
annotation- field annotationfield- annotated fieldtestClass- test class where field was searcher
-
-
Method Detail
-
getAnnotation
public A getAnnotation()
- Returns:
- field annotation instance (type of annotation defined by initial fields search)
-
getType
public java.lang.Class<T> getType()
- Returns:
- field class type
-
getTypeParameters
public java.util.List<java.lang.Class<?>> 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
public java.lang.Class<?> getDeclaringClass()
- Returns:
- class that declares field
-
getName
public java.lang.String getName()
- Returns:
- field name
-
getField
public java.lang.reflect.Field getField()
- Returns:
- field instance
-
getValue
public T getValue(java.lang.Object instance)
- Parameters:
instance- test instance (or null for static field)- Returns:
- test field value
- Throws:
java.lang.IllegalStateException- if non-static field resolved with null instance or other error appear
-
getValue
public T getValue(org.junit.jupiter.api.extension.TestInstances instances)
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
public void setValue(java.lang.Object instance, T value)- Parameters:
instance- test instance (or null for static field)value- field value- Throws:
java.lang.IllegalStateException- if non-static field set with null instance or other error appear
-
setValue
public void setValue(org.junit.jupiter.api.extension.TestInstances instances, T value)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
public java.lang.Object 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
public boolean isCompatible(java.lang.Object instance)
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
public java.lang.Object findRequiredInstance(org.junit.jupiter.api.extension.TestInstances instances)
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:
java.lang.IllegalStateException- if a test instances object provided but does not contain the required test instance
-
setCustomData
public void setCustomData(java.lang.String key, java.lang.Object value)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
public <K> K getCustomData(java.lang.String key)
Get custom value.- Type Parameters:
K- value type- Parameters:
key- key- Returns:
- value or null
-
isCustomDataSet
public boolean isCustomDataSet(java.lang.String key)
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
public java.lang.String toStringField()
- Returns:
- to string field with class and reduced package
-
checkValueNotChanged
public T checkValueNotChanged(org.junit.jupiter.api.extension.TestInstances instance)
Validates that field value not changed (it is the same value as was injected).- Parameters:
instance- test instance- Returns:
- value
- Throws:
java.lang.IllegalStateException- if field value differs from the injected value
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-