Closed ampeixoto closed 3 years ago
It looks like your test is missing the HiltAndroidRule
. Can you try adding HiltAndroidRule
as the outer rule in your test?
Also, I haven't looked into ServiceTestRule
specifically, but I'm guessing it may have the same issues as ActivityScenarioRule
. In particular, ActivityScenarioRule
calls onCreate
and performs injection before you have a chance to configure bindings in @Before
, which can lead to issues, for example if the activity tries to use an injected @BindValue
mock before you've had a chance to configure it (see the warning at the end of this section).
@bcorso thanks for your response.
Even with the HiltAndroidRule
I get the same error:
Updated test
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
@MediumTest
internal class DummyTestServiceTest {
var hiltRule = HiltAndroidRule(this)
val serviceRule = ServiceTestRule()
@get:Rule
var rule = RuleChain.outerRule(hiltRule).around(serviceRule)
@Test
fun testWithStartedService() {
serviceRule.startService(
Intent(
ApplicationProvider.getApplicationContext<Context>(),
DummyTestService::class.java
)
)
// Add your test code here.
}
@Test
fun testWithBoundService() {
val binder = serviceRule.bindService(
Intent(
ApplicationProvider.getApplicationContext<Context>(),
DummyTestService::class.java
)
)
val service = (binder as DummyTestService.LocalBinder).getService()
Assert.assertNotNull(service)
}
}
I even removed the usage of @UninstallModules
in favor of @TestInstallIn
but no luck. Any other suggestion?
Hmm, the error message doesn't really look specific to something in Hilt. Could you check that things work as expected with a non-Hilt service?
It seems it is not related with hilt... I removed any dependency to hilt for this test, (including the application=dagger.hilt.android.testing.HiltTestApplication
config from the robolectric.properties
file) and the error persists.
New version of the test:
@RunWith(AndroidJUnit4::class)
@MediumTest
internal class DummyTestServiceTest {
@get:Rule
val serviceRule = ServiceTestRule()
@Test
fun testWithStartedService() {
serviceRule.startService(
Intent(
ApplicationProvider.getApplicationContext<Context>(),
DummyTestService::class.java
)
)
// Add your test code here.
}
@Test
fun testWithBoundService() {
val app = ApplicationProvider.getApplicationContext<Context>()
val binder = serviceRule.bindService(
Intent(
app,
DummyTestService::class.java
)
)
val service = (binder as DummyTestService.LocalBinder).getService()
Assert.assertNotNull(service)
}
}
And when debugging the second test, I see that the app
is instance of Application
:
Do you have any other suggestion I can try or should I open the ticket on the corresponding repo?
@ampeixoto, unfortunately I'm not familiar with the ServiceTestRule
API, so I don't have any other suggestions. I'll close this ticket. Filing a ticket with the Androidx team or asking on stack overflow may be your best bet.
I am trying to access
ServiceComponent
bindings in a similar way that is described in the section "Accessing ActivityComponent bindings" from the documentation. Of course in the example, it is for activities, but I suppose I should be able to use a similar strategy forServiceComponent
.So I am using ServiceTestRule.
But the problem is that I am not event able to start/bind to the service, I get the following error:
This is my DummyService:
And this is the test skeleton:
Is there some incompatibility between
ServiceTestRule/Robolectric/Hilt
? Let me know if you need some more info.