Annotation Interface StubBean
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 test field
as @StubBean(Service.class) ServiceStub stub; (could be a static filed). 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).
If stub field is initialized manually, then manual instance would be injected into guice context. In case when guicey extension started per class and non-static stub field is initialized, guicey will throw an error (because it is impossible to get non-static field value in time of guice context creation). Pay attention that guice AOP will not be applied to the manually created stub!
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. Stub field must declare interface as a binding key:
@StubBean(IService.class) ServiceStub stub;
Guicey test extension debug option would also activate printing all detected stub fields.
Stub object would not be re-created for each test in case of per-class test (where application created once for
all test methods). If you need to perform some cleanups between tests, stub class must implement
StubLifecycle and it's before() and after() methods
would be called before and after each test method.
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:
- 06.02.2025
-
Required Element Summary
Required Elements
-
Element Details
-
value
Class<?> valueThe class that this stub must override (could be service itself or base interface). Note that stub class can't be the same as overriding class.- Returns:
- replaced service class
-