Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric with AndroidX Fragments

How to test AndroidX fragment with Robolectric?

I added testImplementation "org.robolectric:shadows-supportv4:4.0-alpha-3" dependency and tried with this code:

val controller = SupportFragmentController.setupFragment(
            TestableFragment.buildFragment(DATA),
            TestableFragmentHolderActivity::class.java)

TestableFragment is androidx.fragment.app.Fragment, and TestableFragmentHolderActivity is androidx.appcompat.app.AppCompatActivity

But I get an error during test:

java.lang.NoSuchMethodError: org.robolectric.shadows.support.v4.SupportFragmentController.setupFragment(Landroidx/fragment/app/Fragment;Ljava/lang/Class;)Landroidx/fragment/app/Fragment;

like image 410
Ufkoku Avatar asked Sep 08 '18 12:09

Ufkoku


2 Answers

There is a new way to test fragments with Robolectric's latest API. Check the official doc You'll need to create a FragmentScenario

val fragmentScenario = launchFragmentInContainer<MyFragment>()

and then test like you usually do with Espresso

onView(withId(R.id.text)).check(matches(withText("Hello World!")))
like image 133
fo2rist Avatar answered Oct 29 '22 15:10

fo2rist


Kindly refer to this git issue: https://github.com/robolectric/robolectric/issues/3985. New API for AndroidX fragment will be soon released.

like image 36
Sai Deepthi Avatar answered Oct 29 '22 15:10

Sai Deepthi