'Error: Hilt Fragments must be attached to an @AndroidEntryPoint Activity

Fragment is not loading in Unit Test.

Code:

@RunWith(AndroidJUnit4::class)
class DataFeature  {

    @Test
    fun testEventFragment() {
            val fragmentArgs = bundleOf("test" to "100")
            val scenario = launchFragmentInContainer<DetailFragment>(fragmentArgs)
    }
}

Error:

java.lang.IllegalStateException: Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: class androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity

Fragment file

@AndroidEntryPoint
class DetailFragment : Fragment() {....}

while I am using Hilt in my application code. It's showing error in test case. It's not launching that fragment. I am using jetpack navigation controller as well.

Any help would be appreciated! Thanks in Advance.



Solution 1:[1]

try to downgrade your classpath to

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 3ameration