@CustomTestApplication value cannot be annotated with @HiltAndroidApp
See original GitHub issueI have an application that needs injection:
@HiltAndroidApp
class MyApplication: Application() {
@Inject lateinit var printerFactory: PrinterFactory
...
}
Other code in the app uses application.printerFactory
.
The printerFactory
is provided by this:
@Module
@InstallIn(ApplicationComponent::class)
object ProdModule {
@Provides
fun providePrinterFactory(): PrinterFactory {
return FactoryThatReturnsARealPrinter()
}
}
Now, for instrumented tests, I need to override this with a test version:
@HiltAndroidTest
@UninstallModules(ProdModule::class)
@RunWith(AndroidJUnit4::class)
@LargeTest
class MainTest {
@get:Rule(order = 0)
var hiltRule = HiltAndroidRule(this)
@get:Rule(order = 1)
var intentsTestRule = IntentsTestRule(MainActivity::class.java, false, false)
// Test replacements for production stuff.
@Module
@InstallIn(ApplicationComponent::class)
class TestModule {
@Provides
fun providePrinterFactory(): PrinterFactory {
return FactoryThatReturnsATestPrinter()
}
}
...
}
And so I get this error:
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.
Well, I can’t use HiltTestApplication because I need to use MyApplication, which has dependencies injected.
So I added this:
@CustomTestApplication(MyApplication::class)
interface HiltTestApplication
And so I get this error:
error: [Hilt]
public static abstract interface HiltTestApplication {
^
@CustomTestApplication value cannot be annotated with @HiltAndroidApp. Found: MyApplication
If MyApplication
cannot be annotated with @HiltAndroidApp
, then how is it expected to inject things?
Instructions unclear, ended up in inconsistent state.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:25 (3 by maintainers)
Top Results From Across the Web
CustomTestApplication value cannot be annotated with ...
You will need to create a new class with the @HiltAndroidApp annotation, which would be different from the one you will use in...
Read more >Hilt testing guide | Android Developers
Custom application for tests ... If you cannot use HiltTestApplication because your test application needs to extend another application, annotate a new class...
Read more >Testing with Hilt android - Nyame Bismark - Medium
Custom application for tests. If you cannot use HiltTestApplication because your test application needs to extend another application, annotate a new class or ......
Read more >Hilt Can't Find Application Class Even After Been Annotated ...
As the error indicates adding the annotation @HiltAndroidApp to the AppController @CustomTestApplication value cannot be annotated with @HiltAndroidApp.
Read more >Testing - Hilt - Dagger
In addition, Hilt cannot be used in vanilla JVM tests, but it does not ... with @CustomTestApplication and specify the base class in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Bad Solution Regarding the this documentation I create interface and put my test application:
But it gives me exception:
So i’m directly passing the generated hilt application class for my test app and skip using
AndroidTestApplication
interface and it works:Instrumentation.newApplication(AndroidTestApplication_Application::class.java, context)
@danysantiago I got the following issue when I use a base app. Do I missing something else ?
java.lang.RuntimeException: Unable to instantiate application com.sample.MainApplication: java.lang.InstantiationException: java.lang.Class<com.sample.di.TestApplication> cannot be instantiated