-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hilt: handle Services with injection automatically started in instrumentation tests #2016
Comments
Does "automatic" here mean that it's implicitly started without any way for you to control it? Or is there some initialization method you call at app startup that triggers it? |
Exactly |
Does that mean just adding the library to your |
Yes. A pretty common use case is a |
Ah, I was aware of Unfortunately, there's not much we can do here because creating the Hilt components in tests require an instance of the tests. The issue is that Sorry, I don't have a great solution but here are a couple ideas:
|
Just wanted to chime in to expand on this and explain that requiring the test instance is needed to provide things like |
This suggestion should solve our issue! I always forget about how the I was hoping in a solution that didn't require a change in the production code but I think it's a small cost for a use case like this. Thanks! |
Hey @francescocervone, I am facing the same issue. Just to make sure, what you did is something like this? // Remove this @AndroidEntryPoint
class MyMessagingService : FirebaseMessagingService() {
@EntryPoint
@InstallIn(ApplicationComponent.class)
public interface MyMessagingServiceInterface {
Dep1 getDep1();
Dep2 getDep2();
}
@Override public void onMessageReceived(RemoteMessage remoteMessage) {
// Make sure we gather all the required dependencies
MyMessagingServiceInterface myMessagingServiceInterface = EntryPoints.get(getApplicationContext(), MyMessagingServiceInterface.class);
Dep1 dep1 = myMessagingServiceInterface.getDep1();
Dep2 dep2 = myMessagingServiceInterface.getDep2();
...
}
// Probably something similar in onNewToken()
...
} |
@sjaramillo10 yes but it doesn't solve the problem. There is still a race condition that makes the test crash when onNewToken gets invoked. |
@francescocervone thanks for replying! And yes, you are right. I had a crash now in the |
No, except some horrible workaround like surrounding EntryPoints.get with a try catch or checking some static Boolean for tests only... |
FWIW, we're considering loosening these restrictions a bit. Basically, we want to allow you to use |
…before the test instance is instantiated. See #2016 RELNOTES=Add EarlyTestEntryPoints to allow entry points to be called in tests before the test instance is instantiated. PiperOrigin-RevId: 356184068
…before the test instance is instantiated. See #2016 RELNOTES=Add EarlyTestEntryPoints to allow entry points to be called in tests before the test instance is instantiated. PiperOrigin-RevId: 356184068
…before the test instance is instantiated. See #2016 RELNOTES=Add EarlyTestEntryPoints to allow entry points to be called in tests before the test instance is instantiated. PiperOrigin-RevId: 356184068
…before the test instance is instantiated. See #2016 RELNOTES=Add EarlyTestEntryPoints to allow entry points to be called in tests before the test instance is instantiated. PiperOrigin-RevId: 356184068
…before the test instance is instantiated. See #2016 RELNOTES=Add EarlyTestEntryPoints to allow entry points to be called in tests before the test instance is instantiated. PiperOrigin-RevId: 359350904
We use app-startup library for initialization of different stuff like WorkManager and we use this
But obviously it does not work for tests. What we tried is to use EarlyEntryPoint
And now Any ideas how it could be fixed? |
Do you have a full stacktrace? Are you running this as a local (e.g. robolectric) or instrumentation (e.g. espresso) test? Also, since this error is not directly caused by Hilt (i.e. we don't throw it) it may be worth searching/asking on stackoverflow for more generally what causes this problem. For example, from a quick search, it seems like that error is sometimes caused when mixing |
@bcorso the original issue is fixed by using a combination of @ReginFell usually the 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. |
@francescocervone, thanks for the update! I'll go ahead and close this now. |
@ReginFell were you able to solve this issue? |
Unfortunately, this is still an issue because UI tests fail when using a |
@levon93 looks like the original issue was solved (#2016 (comment)). If you are still having issues, can you please open a new issue with more details. |
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.Then I have a
@HiltAndroidTest
that runshiltRule.inject()
in its@Before
function.Unfortunately, the way Hilt injection works in tests generates a race condition between
hiltRule.inject()
andMyMessagingService#onCreate
. If the latter gets executed beforehiltRule.inject()
the test will crash because dependency graph has not been created yet.How can I avoid this issue?
The text was updated successfully, but these errors were encountered: