Class LogFieldsSupport
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.jupiter.env.field.AnnotatedTestFieldSetup<RecordLogs,RecordedLogs>
-
- ru.vyarus.dropwizard.guice.test.jupiter.ext.log.LogFieldsSupport
-
- All Implemented Interfaces:
TestExecutionListener,TestEnvironmentSetup
public class LogFieldsSupport extends AnnotatedTestFieldSetup<RecordLogs,RecordedLogs>
RecordLogstest fields support implementation.Applies custom appenders into required logback loggers. If required, lower loggers level (to receive all required events). Note that appender applied three times: before application starts, before init (because app creation reset logs) and in dropwizard run phase (because dropwizard would reset loggers during startup).
By default, collected logs cleared after each test method.
- Since:
- 26.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 LogFieldsSupport()Create support.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafterTest(EventContext context, AnnotatedField<RecordLogs,RecordedLogs> field, RecordedLogs value)Called after each test to post-process field value (if required).protected voidbeforeTest(EventContext context, AnnotatedField<RecordLogs,RecordedLogs> field, RecordedLogs value)Called before each test to pre-process field value (if required).protected voidbeforeValueInjection(EventContext context, AnnotatedField<RecordLogs,RecordedLogs> field)Called after application startup and before field value injection.protected voidfieldDetected(org.junit.jupiter.api.extension.ExtensionContext context, AnnotatedField<RecordLogs,RecordedLogs> field)Validate resolved field, if required.protected <K> voidinitializeField(AnnotatedField<RecordLogs,RecordedLogs> field, RecordedLogs userValue)Configure application for a field (user value might be provided).protected RecordedLogsinjectFieldValue(EventContext context, AnnotatedField<RecordLogs,RecordedLogs> 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<RecordLogs,RecordedLogs>> annotatedFields)Called when debug is enabled on guicey extension to report registered fields.voidstopped(EventContext context)Called when dropwizard (or guicey) application stopped.-
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, 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.test.jupiter.env.listen.TestExecutionListener
afterAll, stopping
-
-
-
-
Method Detail
-
fieldDetected
protected void fieldDetected(org.junit.jupiter.api.extension.ExtensionContext context, AnnotatedField<RecordLogs,RecordedLogs> 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<RecordLogs,RecordedLogs>- Parameters:
context- junit contextfield- annotated fields
-
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<RecordLogs,RecordedLogs>- Parameters:
extension- extension configuration object
-
initializeField
protected <K> void initializeField(AnnotatedField<RecordLogs,RecordedLogs> field, RecordedLogs 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<RecordLogs,RecordedLogs>- 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<RecordLogs,RecordedLogs> 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<RecordLogs,RecordedLogs>- Parameters:
context- event contextfield- annotated field
-
injectFieldValue
protected RecordedLogs injectFieldValue(EventContext context, AnnotatedField<RecordLogs,RecordedLogs> 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<RecordLogs,RecordedLogs>- Parameters:
context- event contextfield- annotated field- Returns:
- created field value
-
report
protected void report(EventContext context, java.util.List<AnnotatedField<RecordLogs,RecordedLogs>> 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<RecordLogs,RecordedLogs>- Parameters:
context- event context, IMPORTANT - this would be setup context and not currentannotatedFields- fields to report
-
beforeTest
protected void beforeTest(EventContext context, AnnotatedField<RecordLogs,RecordedLogs> field, RecordedLogs value)
Description copied from class:AnnotatedTestFieldSetupCalled before each test to pre-process field value (if required).- Specified by:
beforeTestin classAnnotatedTestFieldSetup<RecordLogs,RecordedLogs>- Parameters:
context- event contextfield- filed descriptorvalue- value instance
-
afterTest
protected void afterTest(EventContext context, AnnotatedField<RecordLogs,RecordedLogs> field, RecordedLogs value)
Description copied from class:AnnotatedTestFieldSetupCalled after each test to post-process field value (if required).- Specified by:
afterTestin classAnnotatedTestFieldSetup<RecordLogs,RecordedLogs>- Parameters:
context- event contextfield- filed descriptorvalue- value instance
-
stopped
public void stopped(EventContext context)
Description copied from interface:TestExecutionListenerCalled when dropwizard (or guicey) application stopped. It could be afterAll or afterEach phase (if important, lookExtensionContext.getTestMethod()to make sure). Application could start/stop multiple times within one test class (if extension registered in non-static field).Note that in case of global application usage or for nested tests this method might not be called because application lifecycle would be managed by the top-most test.
This method could be used instead of afterAll because normally extension is stopped under afterAll, but for extensions stopped under afterEach - it would be impossible to notify about afterAll anyway.
- Specified by:
stoppedin interfaceTestExecutionListener- Overrides:
stoppedin classAnnotatedTestFieldSetup<RecordLogs,RecordedLogs>- Parameters:
context- context object providing access to all required objects (junit context, injector, test support, etc.)
-
-