I am trying to override an existing binding with a Mock instance so I can verify method calls. The TaskManager class in question is annotated with @Inject @Singleton. The Injector is being created in a static @BeforeClass method in JUnit5. The mock is created in a static field, initialized at the declaration.
My first try added the following to an OverrideModule:
new AbstractModule() {
@Override
protected void configure() {
bind(TaskManager.class).toInstance(mockTaskManager);
}
}
I think this didn't work because of the JIT binding. So I added a binding to the real module.
bind(TaskManager.class).in(Singleton.class);
Still didn't work, so I tried a different override binding, trying to match the scope
new AbstractModule() {
@Override
protected void configure() {
bind(TaskManager.class)
.toProvider(Providers.of(taskManager))
.in(Singleton.class);
}
}
I still get the real implementation of TaskManager from the Injector.
Is there any way to do what I need to do?
Thank you,
Jim McMaster