Class MocksHook
- All Implemented Interfaces:
GuiceyConfigurationHook
Important: requires mockito dependency!
Usage example:
MocksHook hook = new MocksHook();
Service mock = hook.mock(Service.class);
when(mock.foo()).thenReturn(..)
Could also be used for spy objects registration of beans bound by instance (!)
(so SpiesHook could not be used):
Service spy = hook.mock(Service.class, Mockito.spy(new Service())).
Spy should also be used when mock must be created from an abstract class (preserving abstract methods):
AbstractService mock = hook.mock(AbstractService.class, Mockito.spy(AbstractService.class)).
Limitation: any aop, applied to the original bean, will not work with mock (because guice can't apply aop to
instances)! Use SpiesHook instead if aop is important.
Does not work for HK2 beans.
- Since:
- 29.04.2025
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidconfigure(GuiceBundle.Builder builder) Configuration is applied just after manual configuration (through bundle's builder in application class).<T> T<T> TOverride gucie bean with a mock instance.<T> TOverride guice bean with a user-provided mock instance.voidReset all registered mocks.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook
register
-
Constructor Details
-
MocksHook
public MocksHook()
-
-
Method Details
-
configure
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:
Exception- on error (simplify usage)
- Generic disable:
-
mock
Override gucie bean with a mock instance.- Type Parameters:
T- bean type- Parameters:
type- bean type- Returns:
- mock instance
-
mock
Override guice bean with a user-provided mock instance.- Type Parameters:
T- bean type- Parameters:
type- bean typemock- mock instance- Returns:
- passed mock instance
-
getMock
- Type Parameters:
T- bean type- Parameters:
type- bean type- Returns:
- mock instance registered for bean type
- Throws:
IllegalStateException- if mock for type is not registered
-
resetMocks
public void resetMocks()Reset all registered mocks.
-