-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hilt: Supporting module uninstallation across all tests #1923
Comments
For now, the best approach is to use Also, see #1896 |
An interesting thought I wanted to provide in case it had not been considered: Since the @UninstallModules(ApiConfigurationModule::class)
@HiltAndroidTest
open class BaseHiltTest {
@get:Rule
val hiltRule by lazy { HiltAndroidRule(this) } // lazy to prevent leaking this in non-final class
// If the developer needs any special setup config
open fun setup() {}
@Before
fun init() {
hiltRule.inject()
setup() // this pattern allows a dev to still call code before without remembering to call a super
}
} Downsides to the approach: I believe adding another method annotated with I don't think my solution is fully capable of handling those cases, but maybe it will give ideas to others. |
I've got a case where adding It would be ideal to uninstall a module via the test application class. I need to do some more investigation on how this is implemented but I need the ability to swap in and out modules on an application to application basis for shared code. |
+1, this would really be helpful for my codebase. |
Hi @krgauthi,
Can you clarify what you mean by "doesn't know about the module"? If the feature doesn't depend on the application module then it shouldn't be installed, so there would be no need to uninstall it.
Can you describe where your test vs test application live? Are you talking about a case where the test application is in a separate Gradle module from the test? |
@bcorso This could be a lengthy explanation, I might need to make an example.
I've got two Gradle modules one for each app, I've got a third gradle module with a shared feature from both apps that's a dependency for each app. I've currently got my Espresso tests in that feature gradle module. With Dagger I had two AppComponents for each app (4 total), one for normal builds the other for integration tests. The difference between the components was just one dagger module one had real implementations the other had stubbed implementations for the integration tests. My issue with adding I think that this is a similar issue |
This feature allows users to replace @Installin modules with a @TestInstallIn module. For example, you can replace FooModule with FakeFooModule with the following: ``` @module @TestInstallIn( components = SingletonComponent.class, replaces = FooModule.class) interface FakeFooModule { ... } ``` Note that @TestInstallIn only replaces @Installin when using @HiltAndroidTest, it does not apply in @HiltAndroidApp. RELNOTES=Adds @TestInstallIn feature to Hilt PiperOrigin-RevId: 351213437
Closing this as the above commit should fix this issue. |
From https://developer.android.com/training/dependency-injection/hilt-testing:
This is in reference to providing modules within a test class vs at the top level. Moving it at the top level works, but it seems like every test is still required to annotate production modules to be uninstalled. The annotation is ignored if we try to use it in an abstract super class for all tests.
What is the approach here? Should we try to also add a hilt rule in the super class? Is there a workaround to avoid uninstalling the same module across all tests?
The text was updated successfully, but these errors were encountered: