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
On Thursday, November 9, 2023 at 2:08:13 PM UTC-7 Jim McMaster wrote:
> 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
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/428c1741-b1ab-4b77-a568-169f8caaa3adn%40googlegroups.com.