Problem trying to override a binding (currently a JIT singleton)

40 views
Skip to first unread message

Jim McMaster

unread,
Nov 9, 2023, 4:08:13 PM11/9/23
to google-guice
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

Jim McMaster

unread,
Nov 9, 2023, 5:26:15 PM11/9/23
to google-guice
I'm sorry, I should have mentioned.  I am using Guice 4.2.2.  I can update to a newer version if that would help
Reply all
Reply to author
Forward
0 new messages