auto-dagger icon indicating copy to clipboard operation
auto-dagger copied to clipboard

@Replaces on fields

Open F43nd1r opened this issue 8 months ago • 2 comments

Is your feature request related to a problem? Please describe. I'm not sure if this is possible, but it'd be nice to be able to use @Replaces on fields as well

Describe the solution you'd like Currently I'd write

@UninstallModules(AutoBindExampleImplSingletonModule::class)
@HiltAndroidTest
class ExampleTest {
    @JvmField
    @BindValue
    val example = mockk<Example>()
}

to inject a mock in a single test. It'd be nice to write

@HiltAndroidTest
class ExampleTest {
    @JvmField
    @Replaces(ExampleImpl::class)
    val example = mockk<Example>()
}

instead.

F43nd1r avatar Mar 31 '25 10:03 F43nd1r

This is an interesting idea, I did not know about @BindValue. I don't think it's possible to do exactly what you suggest because you'd still need to have @BindValue on the property. But it might be possible to make something that is better.

It's also possible that the test is an input to the graph, in which case this would be possible.

ansman avatar Mar 31 '25 13:03 ansman

I got some time to look into this but I don't think this is possible unfortunately. The generated code cannot use @UninstallModules because that can only be added to the test class and @TestInstallIn is global in the module. There is also no way to scope the module to the test class as it needs to be nested for that to work. I'll keep this open to think about it some more

ansman avatar Jun 15 '25 01:06 ansman