Annotation Type SpyBean


  • @Retention(RUNTIME)
    @Target(FIELD)
    public @interface SpyBean
    Replace any guice service with mockito spy in test. The difference with mock: spy wraps around the real service(!) and could be used to validate called service methods (verify incoming parameters and output value).

    Important: requires mockito dependency!

    In contrast to mocks and stubs, spies work with guice AOP: all calls to service are intercepted and passed through the spy object (as "proxy"). That also means that all aop, applied to the original bean, would work (in contrast to mocks).

    As spy requires real bean instance - spy object is created just after injector creation (and AOP interceptor redirects into it (then real bean called)). Spy object, injected to a field would not be the same instance as injected bean (@Inject SpiedService) because injected bean would be a proxy, handling guice AOP.

    Calling bean methods directly on spy is completely normal (guice bean just redirects calls to spy object)!

    Example: @SpyBean Service spy. May be used for static and instance fields.

    Spy field CAN'T be initialized manually. Use MockBean or StubBean instead for manual spy objects initialization. Such manual initialization could be required for spies created from abstract classes (or multiple interfaces): in this case actual bean instance is not required, and so mocks support could be used instead: @MockBean AbstractService spy = Mockito.spy(AbstractService.class).

    Spy stubs could be configured in test beforeEach method: Mockito.roReturn("ok").when(spy).something().

    Spies reset called before and after each test method. Could be disabled with autoReset()

    Mockito provide the detailed report of used mock methods and redundant stub definitions. Use printSummary() to enable this report (printed after each test method).

    Guicey extension debug (TestGuiceyApp.debug()) enables spy fields debug: all recognized annotated fields would be printed to console.

    If spies assumed to be used only to validate bean in/out, then TrackBean might be used instead: it is simply collects called methods with argument and results, plug measure performance (spies and trackers could be used together).

    Since:
    10.02.2025
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      boolean autoReset
      Note: spy could be reset manually with Mockito.reset(Object[]).
      java.lang.Class<? extends java.util.function.Consumer<?>>[] initializers
      In most cases, spy object could be configured after application startup (using test setUp method).
      boolean printSummary
      Native mockito spy usage report: shows called methods and stubbed, but not used methods.
    • Element Detail

      • autoReset

        boolean autoReset
        Note: spy could be reset manually with Mockito.reset(Object[]).
        Returns:
        true to reset spy after each test method
        Default:
        true
      • printSummary

        boolean printSummary
        Native mockito spy usage report: shows called methods and stubbed, but not used methods.
        Returns:
        true to print spy summary after each test
        Default:
        false
      • initializers

        java.lang.Class<? extends java.util.function.Consumer<?>>[] initializers
        In most cases, spy object could be configured after application startup (using test setUp method). But if target spy is used during application startup, this initializer could be used to configure spy object on first access and so application startup logic (like managed objects) would use already configured spy during startup.

        Consumer accepts already created spy instance.

        Returns:
        optional spy object initializer
        Default:
        {}