Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@CustomTestApplication value cannot be annotated with @HiltAndroidApp

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
like image 346
chaitanya prasad movva Avatar asked Jul 16 '20 03:07

chaitanya prasad movva


People also ask

What is @customtestapplication in hilt?

@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.

How to fix IllegalStateException myapplication cannot use a hiltandroidapp application?

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.

Why can’t I use hilttestapplication?

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.

How do I use hilt with Android integration tests?

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.


1 Answers

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

like image 70
chaitanya prasad movva Avatar answered Nov 09 '22 17:11

chaitanya prasad movva