Class StubFieldsSupport

  • All Implemented Interfaces:
    TestExecutionListener, TestEnvironmentSetup

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

    Annotated fields resolved in time of guicey extension initialization (beforeAll or beforeEach). Register override bindings for provided stubs (singletons!). Stub instances created by guice (to be able to use injections inside it). If stub field is initialized manually - this value would be bound into guice context (see debug report to be sure what value was actually used - field might be assigned too late).

    In beforeAll injects static values, in beforeEach inject both (in case if beforeAll wasn't called).

    For stub objects, implementing StubLifecycle before and after methods called on beforeEach and afterEach to perform cleanups.

    Since:
    07.02.2025
    • Constructor Detail

      • StubFieldsSupport

        public StubFieldsSupport()
        Create support.
    • Method Detail

      • fieldDetected

        protected void fieldDetected​(org.junit.jupiter.api.extension.ExtensionContext context,
                                     AnnotatedField<StubBean,​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<StubBean,​java.lang.Object>
        Parameters:
        context - junit context
        field - annotated fields
      • initializeField

        protected <K> void initializeField​(AnnotatedField<StubBean,​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<StubBean,​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)