Class StubsHook

  • All Implemented Interfaces:
    GuiceyConfigurationHook

    public class StubsHook
    extends java.lang.Object
    implements GuiceyConfigurationHook
    Replace any guice service with its stub in test (using guice module overrides). Consider stubs as a hand-made mocks.

    Example: suppose we have some Service and we need to modify it for tests, so we extend it with class ServiceStub extends Service and override required methods. Register stub in hook as hook.stub(Service.class, ServiceStub.class). Internally, overriding guice binding would be created: bind(Service.class).to(ServiceStub.class).in(Singleton.class) so guice would create stub instead of the original service. Guice would create stub instance, so injections would work inside it (and AOP).

    Stub could also be initialized manually: manual instance would be injected into guice context (annotated fields injection would also be performed for provided instance): hook.stub(Service.class, new ServiceStub()).

    More canonical example with interface: suppose we have bind(IServie.clas).to(ServiceImpl.class)). In this case, stub could simply implement interface, instead of extending class (class ServiceStub implements IService): hook.stub(IService.class, ServiceStub.class);

    Just in case: guice injection will also return stabbed bean (because stub instance is created by guice or instance bound into it).

    Does not work for HK2 beans.

    Since:
    30.04.2025
    • Constructor Summary

      Constructors 
      Constructor Description
      StubsHook()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void after()
      Run StubLifecycle.after() for all stubs, implementing lifecycle interface.
      void before()
      Run StubLifecycle.before() for all stubs, implementing lifecycle interface.
      void configure​(GuiceBundle.Builder builder)
      Configuration is applied just after manual configuration (through bundle's builder in application class).
      <T,​P extends T>
      P
      getStub​(java.lang.Class<T> type)  
      <T> void stub​(java.lang.Class<T> type, java.lang.Class<? extends T> stub)
      Register stub class.
      <T> void stub​(java.lang.Class<T> type, T value)
      Same as stub(Class, Class), but with manually created stub instance.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StubsHook

        public StubsHook()
    • Method Detail

      • stub

        public <T> void stub​(java.lang.Class<T> type,
                             java.lang.Class<? extends T> stub)
        Register stub class. Here stub must either extend original service (class Stub extends Service) or, if target service use interface for binding (bind(IService.class).to(ServiceImpl.class), implement that interface (class Stub implements IService).

        Stub instance would be managed with guice and so guice AOP could be applied for stub.

        If stub implements StubLifecycle, then stubs lifecycle could be emulated with before() and after() methods. Might be used to reset stub state between tests.

        Type Parameters:
        T - service type
        Parameters:
        type - overriding service type
        stub - stub implementation (used to override application service)
      • stub

        public <T> void stub​(java.lang.Class<T> type,
                             T value)
        Same as stub(Class, Class), but with manually created stub instance. In this case, guice AOP will not work for sub instance. Binder.requestInjection(stub) would be called for stub instance to support fields injection.
        Type Parameters:
        T - service type
        Parameters:
        type - overriding service type
        value - stub instance (used to override application service)
      • before

        public void before()
        Run StubLifecycle.before() for all stubs, implementing lifecycle interface. For example, it could be called before each test.
      • after

        public void after()
        Run StubLifecycle.after() for all stubs, implementing lifecycle interface. For example, it could be called after each test.
      • getStub

        public <T,​P extends T> P getStub​(java.lang.Class<T> type)
        Type Parameters:
        T - bean type
        P - stub implementation type
        Parameters:
        type - bean type
        Returns:
        stub instance registered for bean type
        Throws:
        java.lang.IllegalStateException - if stub for type is not registered