Class SpyFieldsSupport

  • All Implemented Interfaces:
    TestExecutionListener, TestEnvironmentSetup

    public class SpyFieldsSupport
    extends AnnotatedTestFieldSetup<SpyBean,​java.lang.Object>
    SpyBean test fields support implementation.

    Annotated fields resolved in time of guicey extension initialization (beforeAll or beforeEach). Register aop interceptor around target service to intercept all calls, and redirect all calls through spy object. This way, real bean becomes spied and still injected everywhere.

    Manual values are not supported: @MockBean should be used instead.

    In beforeAll injects static values, in beforeEach inject both (in case if beforeAll wasn't called). Calls spies reset after each test.

    Since:
    10.02.2025
    • Constructor Detail

      • SpyFieldsSupport

        public SpyFieldsSupport()
        Create support.
    • Method Detail

      • fieldDetected

        protected void fieldDetected​(org.junit.jupiter.api.extension.ExtensionContext context,
                                     AnnotatedField<SpyBean,​java.lang.Object> field)
        Description copied from class: AnnotatedTestFieldSetup
        Validate resolved field, if required. Note that some validations are performed automatically like checking field type with provided required type or unreachable annotated fields reporting. This method should be used for validations, which are not possible to perform automatically (e.g., there is a class, declared in annotation that must comply with a field type (base class know nothing about annotation and can't check that).

        Called only for current test class own fields: in case of nested test, root test fields would already be validated. Also, if guice context started per each test method, validation would be called only for the first test method because fields would be searched just once - no need to validate each time.

        Specified by:
        fieldDetected in class AnnotatedTestFieldSetup<SpyBean,​java.lang.Object>
        Parameters:
        context - junit context
        field - annotated fields
      • initializeField

        protected <K> void initializeField​(AnnotatedField<SpyBean,​java.lang.Object> field,
                                           java.lang.Object userValue)
        Description copied from class: AnnotatedTestFieldSetup
        Configure application for a field (user value might be provided). There might be field object instance creation (e.g. mocks initialization), guice overrides registration, etc. The main initialization point.

        NOTE: If user-provided values are not allowed, throw an exception here

        Specified by:
        initializeField in class AnnotatedTestFieldSetup<SpyBean,​java.lang.Object>
        Type Parameters:
        K - type for aligning a binding key with value types (cheating on guice type checks)
        Parameters:
        field - annotated field
        userValue - user-provided field value (pre-initialized)
      • report

        protected void report​(EventContext context,
                              java.util.List<AnnotatedField<SpyBean,​java.lang.Object>> annotatedFields)
        Description copied from class: AnnotatedTestFieldSetup
        Called when debug is enabled on guicey extension to report registered fields. Note: there might be fields from multiple test classes in case of nested tests.

        Report called after application startup because at this point all fields were processed (in configure guice method) and so all required fields data collected. Called only if at least one field is detected.

        Special custom data markers used in field objects (AnnotatedField.getCustomData(String)):

        Specified by:
        report in class AnnotatedTestFieldSetup<SpyBean,​java.lang.Object>
        Parameters:
        context - event context, IMPORTANT - this would be setup context and not current
        annotatedFields - fields to report