Hilt: handle Services with injection automatically started in instrumentation tests
See original GitHub issueLet’s assume I have a service that is automatically started at the app startup and it is required by some third party library.
This service needs some fields to be injected in the onCreate
method.
@AndroidEntryPoint
class MyMessagingService : FirebaseMessagingService() {
@Inject internal lateinit var dep1: Dep1
@Inject internal lateinit var dep2: Dep2
...
}
Then I have a @HiltAndroidTest
that runs hiltRule.inject()
in its @Before
function.
@AndroidEntryPoint
class MyTest {
@get:Rule val hiltRule = HiltAndroidRule(this)
@Before fun inject() {
hiltRule.inject()
}
...
}
Unfortunately, the way Hilt injection works in tests generates a race condition between hiltRule.inject()
and MyMessagingService#onCreate
. If the latter gets executed before hiltRule.inject()
the test will crash because dependency graph has not been created yet.
How can I avoid this issue?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:18 (1 by maintainers)
Top Results From Across the Web
Hilt testing guide | Android Developers
For integration tests, Hilt injects dependencies as it would in your production code. Testing with Hilt requires no maintenance because Hilt automatically ......
Read more >Testing With Hilt Tutorial: UI and Instrumentation Tests
Learn how to get started with testing with Hilt by writing UI and instrumentation tests.
Read more >Testing - Hilt - Dagger
Hilt makes testing easier by bringing the power of dependency injection to your Android tests. Hilt allows your tests to easily access Dagger...
Read more >Can you UnitTest Android workers that employ Hilt constructor ...
For integration tests, Hilt injects dependencies as it would in your production code. Testing with Hilt requires no maintenance because Hilt ...
Read more >Understanding Dependency Injection with Hilt. - Nyame Bismark
Hilt allows you to use dependency Injection in your app and creates a container of the dependencies and automatically manages the lifecycle of ......
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
@bcorso the original issue is fixed by using a combination of
@EarlyEntryPoint
and the solution proposed here -> https://github.com/google/dagger/issues/2016#issuecomment-685057959@ReginFell usually the
No instrumentation registered
error appears when you are trying to test code dependent on the android framework on the JVM but you forgot to annotate the test class withOf course you need Robolectric in order to make the test pass on the JVM. It should be unrelated to
@EarlyEntryPoint
annotation.FWIW, we’re considering loosening these restrictions a bit.
Basically, we want to allow you to use
EntryPoints.get()
before your test instance is created. While this would lead to a runtime exception if your entry point depends on an@BindValue
, the error message should be pretty clear (e.g. Dagger will tell you that generated@Provides
for the particular@BindValue
field returned null when it should have been non-null).