Fragment's properties are not injected when launching fragment with Robolectric and Dagger Hilt

Viewed 630

Late init fields are not injected in fragment when launch fragment with Robolectric and Dagger Hilt .

I have a following test:

@RunWith(RobolectricTestRunner::class)
@HiltAndroidTest
@Config(application = HiltTestApplication::class)
class SampleTest {

    @Test
    fun checkFragmentProperty() {
        launchFragmentInHiltContainer<TestFragment> {
            // nothing
        }
    }
}

and my TestFragment looks like this:

@AndroidEntryPoint
class TestFragment : Fragment() {

    @Inject
    lateinit var testClass: TestClass

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        testClass.some()
    }
}

When i try to run this test then it displays error kotlin.UninitializedPropertyAccessException: lateinit property testClass has not been initialized

I use launchFragmentInHiltContainer method from the official documentation but the same effect is when i just use lunchFragmentInContainer method from androidx.fragment.app.testing.

When i use emulator instead Robolectric then fields are injected correctly.

I tried to add:

hilt {
    enableTransformForLocalTests = true
}

and run tests from the console but test still fails

0 Answers
Related