Class MocksHook
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.mock.MocksHook
-
- All Implemented Interfaces:
GuiceyConfigurationHook
public class MocksHook extends java.lang.Object implements GuiceyConfigurationHook
Replace any guice service with mockito mock in test (using guice module overrides).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
SpiesHookcould 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
SpiesHookinstead if aop is important. Does not work for HK2 beans.- Since:
- 29.04.2025
-
-
Constructor Summary
Constructors Constructor Description MocksHook()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconfigure(GuiceBundle.Builder builder)Configuration is applied just after manual configuration (through bundle's builder in application class).<T> TgetMock(java.lang.Class<T> type)<T> Tmock(java.lang.Class<T> type)Override gucie bean with a mock instance.<T> Tmock(java.lang.Class<T> type, T mock)Override guice bean with a user-provided mock instance.voidresetMocks()Reset all registered mocks.-
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:
-
mock
public <T> T mock(java.lang.Class<T> type)
Override gucie bean with a mock instance.- Type Parameters:
T- bean type- Parameters:
type- bean type- Returns:
- mock instance
-
mock
public <T> T mock(java.lang.Class<T> type, T 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
public <T> T getMock(java.lang.Class<T> type)
- Type Parameters:
T- bean type- Parameters:
type- bean type- Returns:
- mock instance registered for bean type
- Throws:
java.lang.IllegalStateException- if mock for type is not registered
-
resetMocks
public void resetMocks()
Reset all registered mocks.
-
-