Class 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
      class  TrackersHook.Builder<T>
      Tracker configuration builder.
    • Constructor Summary

      Constructors 
      Constructor Description
      TrackersHook()