You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@AndroidEntryPoint
class MyService() : Service {
// example dependency
@Inject
lateinit var myVariable : String
// class code
}
When we come to write the Robolectric test e.g.
@RunWith(RobolectricTestRunner::class)
@UninstallModules(...)
@Config(application = HiltTestApplication::class)
@HiltAndroidTest
class MediaPlaybackServiceTest {
@Rule @JvmField
val rule : HiltAndroidRule = HiltAndroidRule(this)
@Before
fun setup() {
rule.inject()
// more setup
}
@Test
fun myTest() {
val service : MyService = Robolectric.setupService(MyService::class.java)
// The rest of the test
}
Expect: To be able to generate an instance of MyService in order to test Actual: kotlin.UninitializedPropertyAccessException: lateinit property myVariable has not been initialized
It looks like Robolectric is not using Hilt correctly to set up the service.
Unfortunately, the bytecode injected classes currently don't get picked up on class path when running Robolectric tests via AndroidStudio. We're hoping to fix this once b/37076369 is fixed.
In the meantime, see #1956 for a couple workarounds.
We also plan to explicitly call this out in the docs. I'm in the process of making those changes now.
Given a Service class such as:
When we come to write the Robolectric test e.g.
Expect: To be able to generate an instance of
MyService
in order to testActual:
kotlin.UninitializedPropertyAccessException: lateinit property myVariable has not been initialized
It looks like Robolectric is not using Hilt correctly to set up the service.
Relevant sections of the build.gradle
The text was updated successfully, but these errors were encountered: