Class TrackersHook
- All Implemented Interfaces:
GuiceyConfigurationHook
Useful for validating called method arguments and return value (when service called indirectly - by another top-level
service). In this sense it is very close to mockito spy
(SpiesHook), but api is simpler.
Tracker collects both raw in/out objects and string (snapshot) version (because mutable objects could change).
Raw objects holding could be disabled (TrackersHook.Builder.keepRawObjects(boolean)).
Another use-case is slow methods detection: tracker counts each method execution time, and after test could
print a report indicating the slowest methods. Or it could be used to simply print all called methods to console
with TrackersHook.Builder.trace(boolean) (could be useful during behavior investigations). Another option
is to configure a slow method threshold: then only methods above a threshold would be logged with WARN.
Example usage:
TrackersHook hook = new TrackersHook()
Tracker<Service> tracker = hook.track(Service.class)
// optional configuration
.add()
// after service methods execution
tracker.getTracks()
Tracking is implemented with a custom AOP handler which intercepts all bean calls and record them. Can be used together with mocks, spies or stubs
Limitation: could track only beans, created by guice (due to used AOP). Does not work for HK2 beans.
- Since:
- 28.04.2025
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidconfigure(GuiceBundle.Builder builder) Configuration is applied just after manual configuration (through bundle's builder in application class).<T> Tracker<T>getTracker(Class<T> type) voidClear recorded data for all trackers.<T> TrackersHook.Builder<T>Start bean tracker registration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook
register
-
Constructor Details
-
TrackersHook
public TrackersHook()
-
-
Method Details
-
configure
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:
Exception- on error (simplify usage)
- Generic disable:
-
track
Start bean tracker registration.- Type Parameters:
T- bean type- Parameters:
type- bean type- Returns:
- builder to configure tracker
- Throws:
IllegalStateException- if tracker for bean already registered
-
getTracker
- Type Parameters:
T- bean type- Parameters:
type- bean type- Returns:
- bean tracker instance
- Throws:
IllegalStateException- if tracker for bean is not registered
-
resetTrackers
public void resetTrackers()Clear recorded data for all trackers.
-