Class RestStubFieldsSupport
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.jupiter.env.field.AnnotatedTestFieldSetup<StubRest,RestClient>
-
- ru.vyarus.dropwizard.guice.test.jupiter.ext.rest.RestStubFieldsSupport
-
- All Implemented Interfaces:
GuiceyConfigurationHook,TestExecutionListener,TestEnvironmentSetup
public class RestStubFieldsSupport extends AnnotatedTestFieldSetup<StubRest,RestClient> implements GuiceyConfigurationHook
StubRestfield support implementation.Only one annotated field is supported. Ready to use rest client injected as field value.
By default, all rest resources and jersey extensions registered in application are started. Dropwizard default extensions are also registered - so "stub" completely reproduce application state.
As rest stub ignores web extensions (servlets, filters) then guicey disables all web extensions to avoid confusion (by logged installed extensions).
Jersey container is started just after guicey bundle processing (and not in junit beforeAll) to support guicey jersey report, which is reported just after application initialization (before beforeAll call).
AnnotatedTestFieldSetup used because of implemented Nested classes workflow (so nested class could see a rest client declared in root).
- Since:
- 20.02.2025
-
-
Field Summary
-
Fields inherited from class ru.vyarus.dropwizard.guice.test.jupiter.env.field.AnnotatedTestFieldSetup
appPerClass, FIELD_INJECTED, FIELD_MANUAL, fields, regTestClass, setupContext, setupContextName
-
-
Constructor Summary
Constructors Constructor Description RestStubFieldsSupport()Create support.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafterTest(EventContext context, AnnotatedField<StubRest,RestClient> field, RestClient value)Called after each test to post-process field value (if required).protected voidbeforeTest(EventContext context, AnnotatedField<StubRest,RestClient> field, RestClient value)Called before each test to pre-process field value (if required).protected voidbeforeValueInjection(EventContext context, AnnotatedField<StubRest,RestClient> field)Called after application startup and before field value injection.voidconfigure(GuiceBundle.Builder builder)Configuration is applied just after manual configuration (through bundle's builder in application class).protected voidfieldDetected(org.junit.jupiter.api.extension.ExtensionContext context, AnnotatedField<StubRest,RestClient> field)Validate resolved field, if required.protected <K> voidinitializeField(AnnotatedField<StubRest,RestClient> field, RestClient userValue)Configure application for a field (user value might be provided).protected RestClientinjectFieldValue(EventContext context, AnnotatedField<StubRest,RestClient> field)Get test field value (would be immediately injected into the test field).protected voidregisterHooks(TestExtension extension)Called to register additional guicey hooks, if required.protected voidreport(EventContext context, java.util.List<AnnotatedField<StubRest,RestClient>> annotatedFields)Called when debug is enabled on guicey extension to report registered fields.-
Methods inherited from class ru.vyarus.dropwizard.guice.test.jupiter.env.field.AnnotatedTestFieldSetup
afterEach, beforeAll, beforeEach, failIfInstanceFieldInitialized, getClassContext, getDeclarationErrorPrefix, getOwnFields, getParentFields, getStore, injectValues, isInstanceBinding, lookupFields, setup, started, starting, stopped, validateUnreachableFieldsInNestedTest, valueLifecycle
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook
register
-
Methods inherited from interface ru.vyarus.dropwizard.guice.test.jupiter.env.listen.TestExecutionListener
afterAll, stopping
-
-
-
-
Method Detail
-
configure
public void configure(GuiceBundle.Builder builder) throws java.lang.Exception
Description copied from interface:GuiceyConfigurationHookConfiguration is applied just after manual configuration (through bundle's builder in application class).GuiceBundle.Buildercontains special methods for test support:- Generic disable:
GuiceBundle.Builder.disable(java.util.function.Predicate[]) - Direct disable* method, for example
GuiceBundle.Builder.disableExtensions(Class[]) - Guice bindings override:
GuiceBundle.Builder.modulesOverride(com.google.inject.Module...)
GuiceBundle.Builder.option(Enum, Object)).All configuration items, registered with hook will be scoped as
GuiceyConfigurationHookinstead ofApplicationand so will be clearly distinguishable in configuration logs (GuiceBundle.Builder.printDiagnosticInfo()).- Specified by:
configurein interfaceGuiceyConfigurationHook- Parameters:
builder- just created bundle's builder- Throws:
java.lang.Exception- on error (simplify usage)
- Generic disable:
-
registerHooks
protected void registerHooks(TestExtension extension)
Description copied from class:AnnotatedTestFieldSetupCalled to register additional guicey hooks, if required. Called only when at least one annotated field is detected.- Specified by:
registerHooksin classAnnotatedTestFieldSetup<StubRest,RestClient>- Parameters:
extension- extension configuration object
-
fieldDetected
protected void fieldDetected(org.junit.jupiter.api.extension.ExtensionContext context, AnnotatedField<StubRest,RestClient> field)Description copied from class:AnnotatedTestFieldSetupValidate 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:
fieldDetectedin classAnnotatedTestFieldSetup<StubRest,RestClient>- Parameters:
context- junit contextfield- annotated fields
-
initializeField
protected <K> void initializeField(AnnotatedField<StubRest,RestClient> field, RestClient userValue)
Description copied from class:AnnotatedTestFieldSetupConfigure 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:
initializeFieldin classAnnotatedTestFieldSetup<StubRest,RestClient>- Type Parameters:
K- type for aligning a binding key with value types (cheating on guice type checks)- Parameters:
field- annotated fielduserValue- user-provided field value (pre-initialized)
-
beforeValueInjection
protected void beforeValueInjection(EventContext context, AnnotatedField<StubRest,RestClient> field)
Description copied from class:AnnotatedTestFieldSetupCalled after application startup and before field value injection. Useful for additional validations, which can't be performed before, like binding correctness validation (requiring the created injector): for example, to detect instance bindings when extension relies on AOP and so would not work. Such validation is impossible to do before (in time of binding overrides).Called before
AnnotatedTestFieldSetup.injectFieldValue(ru.vyarus.dropwizard.guice.test.jupiter.env.listen.EventContext, AnnotatedField). At this point, non-static fields could be resolved (test instance present).Important: method called for all fields, even initialized by user! Inject value method might not be called after it!
- Specified by:
beforeValueInjectionin classAnnotatedTestFieldSetup<StubRest,RestClient>- Parameters:
context- event contextfield- annotated field
-
injectFieldValue
protected RestClient injectFieldValue(EventContext context, AnnotatedField<StubRest,RestClient> field)
Description copied from class:AnnotatedTestFieldSetupGet test field value (would be immediately injected into the test field). Called only if field was not initialized by user (not a manual value). For example, implementation might simply get bean instance from guice context (if guice was re-configured with module overrides).Warning: not called for manually initialized fields (because value already set)! To validate binding use
AnnotatedTestFieldSetup.beforeValueInjection(ru.vyarus.dropwizard.guice.test.jupiter.env.listen.EventContext, AnnotatedField)method instead (which is called for all fields).Application already completely started and test extension initialized at this moment (beforeEach test phase).
- Specified by:
injectFieldValuein classAnnotatedTestFieldSetup<StubRest,RestClient>- Parameters:
context- event contextfield- annotated field- Returns:
- created field value
-
report
protected void report(EventContext context, java.util.List<AnnotatedField<StubRest,RestClient>> annotatedFields)
Description copied from class:AnnotatedTestFieldSetupCalled 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)):AnnotatedTestFieldSetup.FIELD_MANUALfield value was initialized by user, otherwise automaticAnnotatedTestFieldSetup.FIELD_INJECTEDfield injection instance (test instance)
- Specified by:
reportin classAnnotatedTestFieldSetup<StubRest,RestClient>- Parameters:
context- event context, IMPORTANT - this would be setup context and not currentannotatedFields- fields to report
-
beforeTest
protected void beforeTest(EventContext context, AnnotatedField<StubRest,RestClient> field, RestClient value)
Description copied from class:AnnotatedTestFieldSetupCalled before each test to pre-process field value (if required).- Specified by:
beforeTestin classAnnotatedTestFieldSetup<StubRest,RestClient>- Parameters:
context- event contextfield- filed descriptorvalue- value instance
-
afterTest
protected void afterTest(EventContext context, AnnotatedField<StubRest,RestClient> field, RestClient value)
Description copied from class:AnnotatedTestFieldSetupCalled after each test to post-process field value (if required).- Specified by:
afterTestin classAnnotatedTestFieldSetup<StubRest,RestClient>- Parameters:
context- event contextfield- filed descriptorvalue- value instance
-
-