Class TrackersHook
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.track.TrackersHook
-
- All Implemented Interfaces:
GuiceyConfigurationHook
public class TrackersHook extends java.lang.Object implements GuiceyConfigurationHook
Track method calls on any guice bean and records arguments and return values, together with measuring time.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 Modifier and Type Class Description classTrackersHook.Builder<T>Tracker configuration builder.
-
Constructor Summary
Constructors Constructor Description TrackersHook()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconfigure(GuiceBundle.Builder builder)Configuration is applied just after manual configuration (through bundle's builder in application class).<T> Tracker<T>getTracker(java.lang.Class<T> type)voidresetTrackers()Clear recorded data for all trackers.<T> TrackersHook.Builder<T>track(java.lang.Class<T> type)Start bean tracker registration.-
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
-
-
-
-
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:
-
track
public <T> TrackersHook.Builder<T> track(java.lang.Class<T> type)
Start bean tracker registration.- Type Parameters:
T- bean type- Parameters:
type- bean type- Returns:
- builder to configure tracker
- Throws:
java.lang.IllegalStateException- if tracker for bean already registered
-
getTracker
public <T> Tracker<T> getTracker(java.lang.Class<T> type)
- Type Parameters:
T- bean type- Parameters:
type- bean type- Returns:
- bean tracker instance
- Throws:
java.lang.IllegalStateException- if tracker for bean is not registered
-
resetTrackers
public void resetTrackers()
Clear recorded data for all trackers.
-
-