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

810 Views Asked by At

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.

1

There are 1 best solutions below

0
3ameration On

try to downgrade your classpath to

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