Class StubsHook

java.lang.Object
ru.vyarus.dropwizard.guice.test.stub.StubsHook
All Implemented Interfaces:
GuiceyConfigurationHook

public class StubsHook extends 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 Details

    • StubsHook

      public StubsHook()
  • Method Details

    • configure

      public void configure(GuiceBundle.Builder builder) throws Exception
      Description copied from interface: GuiceyConfigurationHook
      Configuration is applied just after manual configuration (through bundle's builder in application class).

      GuiceBundle.Builder contains special methods for test support:

      All other configuration options are also available, so it is possible to register extra extensions, bundles etc. or modify guicey options (GuiceBundle.Builder.option(Enum, Object)).

      All configuration items, registered with hook will be scoped as GuiceyConfigurationHook instead of Application and so will be clearly distinguishable in configuration logs (GuiceBundle.Builder.printDiagnosticInfo()).

      Specified by:
      configure in interface GuiceyConfigurationHook
      Parameters:
      builder - just created bundle's builder
      Throws:
      Exception - on error (simplify usage)
    • stub

      public <T> void stub(Class<T> type, 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(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(Class<T> type)
      Type Parameters:
      T - bean type
      P - stub implementation type
      Parameters:
      type - bean type
      Returns:
      stub instance registered for bean type
      Throws:
      IllegalStateException - if stub for type is not registered