Class AnnotatedField<A extends Annotation,T>

java.lang.Object
ru.vyarus.dropwizard.guice.test.jupiter.env.field.AnnotatedField<A,T>
Type Parameters:
A - annotation type
T - value type

public class AnnotatedField<A extends Annotation,T> extends Object
Annotated field wrapper. Used to simplify work with test fields by hiding all required reflection.
Since:
07.02.2025
  • Constructor Details

    • AnnotatedField

      public AnnotatedField(A annotation, Field field, Class<?> testClass)
      Create annotated field for exact field and annotation.
      Parameters:
      annotation - field annotation
      field - annotated field
      testClass - test class where field was searcher
  • Method Details

    • getAnnotation

      public A getAnnotation()
      Returns:
      field annotation instance (type of annotation defined by initial fields search)
    • getType

      public Class<T> getType()
      Returns:
      field class type
    • getTypeParameters

      public List<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 Class<?> getDeclaringClass()
      Returns:
      class that declares field
    • getName

      public String getName()
      Returns:
      field name
    • getField

      public Field getField()
      Returns:
      field instance
    • getValue

      public T getValue(Object instance)
      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

      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(Object instance, T value)
      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

      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 instances
      value - 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 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(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 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:
      IllegalStateException - if a test instances object provided but does not contain the required test instance
    • setCustomData

      public void setCustomData(String key, 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 - key
      value - value
    • getCustomData

      public <K> K getCustomData(String key)
      Get custom value.
      Type Parameters:
      K - value type
      Parameters:
      key - key
      Returns:
      value or null
    • isCustomDataSet

      public boolean isCustomDataSet(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 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:
      IllegalStateException - if field value differs from the injected value
    • toString

      public String toString()
      Overrides:
      toString in class Object