Class MocksHook

java.lang.Object
ru.vyarus.dropwizard.guice.test.mock.MocksHook
All Implemented Interfaces:
GuiceyConfigurationHook

public class MocksHook extends 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 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