question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hilt: handle Services with injection automatically started in instrumentation tests

See original GitHub issue

Let’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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
francescocervonecommented, Mar 3, 2021

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

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class TestClass {  }

Of course you need Robolectric in order to make the test pass on the JVM. It should be unrelated to @EarlyEntryPoint annotation.

3reactions
bcorsocommented, Dec 7, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found