Class StubsHook
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.stub.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
Serviceand we need to modify it for tests, so we extend it withclass ServiceStub extends Serviceand override required methods. Register stub in hook ashook.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 voidafter()RunStubLifecycle.after()for all stubs, implementing lifecycle interface.voidbefore()RunStubLifecycle.before()for all stubs, implementing lifecycle interface.voidconfigure(GuiceBundle.Builder builder)Configuration is applied just after manual configuration (through bundle's builder in application class).<T,P extends T>
PgetStub(java.lang.Class<T> type)<T> voidstub(java.lang.Class<T> type, java.lang.Class<? extends T> stub)Register stub class.<T> voidstub(java.lang.Class<T> type, T value)Same asstub(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
-
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:
-
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 withbefore()andafter()methods. Might be used to reset stub state between tests.- Type Parameters:
T- service type- Parameters:
type- overriding service typestub- stub implementation (used to override application service)
-
stub
public <T> void stub(java.lang.Class<T> type, T value)Same asstub(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 typevalue- stub instance (used to override application service)
-
before
public void before()
RunStubLifecycle.before()for all stubs, implementing lifecycle interface. For example, it could be called before each test.
-
after
public void after()
RunStubLifecycle.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 typeP- 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
-
-