If the Application has a Custom Application object. It is needed to annotate this with @HiltAndroidApp ex: @HiltAndroidApp class AppCore: Application
Appcore has some initialization logic which is needed for the app to run
Now in the Instrumentation tests We also need to Extend the custom Application object. @CustomTestApplication(AppCore::class) interface HiltTestApplication
This gives an error @CustomTestApplication value cannot be annotated with @HiltAndroidApp
Is there any other way of using HILT in instrumentation tests with custom Application objects
public abstract interface HiltTestApplication {
^
@CustomTestApplication value cannot be annotated with @HiltAndroidApp. Found: AppCore
@CustomTestApplication will generate an Application class ready for testing with Hilt that extends the application you passed as a parameter. In the example, Hilt generates an Application named HiltTestApplication_Application that extends the BaseApplication class.
java.lang.IllegalStateException: Hilt test, MainTest, cannot use a @HiltAndroidApp application but found MyApplication. To fix, configure the test to use HiltTestApplication or a custom Hilt test application generated with @CustomTestApplication.
If you cannot use HiltTestApplication because your test application needs to extend another application, annotate a new class or interface with @CustomTestApplication, passing in the value of the base class you want the generated Hilt application to extend.
For integration tests, Hilt injects dependencies as it would in your production code. Testing with Hilt requires no maintenance because Hilt automatically generates a new set of components for each test. To use Hilt in your tests, include the hilt-android-testing dependency in your project: // For Robolectric tests. // ...with Kotlin.
As suggested in the issue tracker. Can you abstract your initialization logic into a base class, say BaseAppCore : Application
then in your prod application extend it @HiltAndroidApp AppCore : BaseAppCore
and then for tests make Hilt generate a test app based on your abstract one, @CustomTestApplication(BaseAppCore::class) interface AppCoreTestApplication
. It might be best to file this issue in https://github.com/google/dagger/issues
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With