Sunday 15 November 2020

How to easily replace production Hilt modules with fakes in all tests

My Android production is code full of Hilt modules that install various production implementations:

@Module
@InstallIn(ApplicationComponent.class)
public abstract class TimeModule {...}
@Module
@InstallIn(ApplicationComponent.class)
public abstract class DatabaseModule {...}

In all my instrumented tests, I would like to replace those bindings with fakes. My test codebase includes modules that bind fake implementations, but having two modules provide the same class obviously causes compile-time errors.

The Hilt documentation recommends using @UninstallModule(), but that means I'd have to add UninstallModule for every single production module in every single test. That seems like the wrong approach.

How would one normally replace production modules with fake modules? And is there a way to install modules from another module like Guice does, so I could remove @InstallIn from all my production modules and instead simply have one ProductionModule that installs all the individual modules? That would make it easier to just uninstall one module in tests.



from How to easily replace production Hilt modules with fakes in all tests

No comments:

Post a Comment