Category : android-testing

How do you launch a fragment in the test scope that contains a LiveData observer with Robolectric. My fragment is observing the liveData from the ViewModel. If I remove this section of code the test runs. fun observerData() { viewModel.MyLiveData.observe(viewLifecycleOwner, Observer { … }) } The test uses a fragment scenario to start the fragment. ..

Read more

How to test a LiveData observer in the scope test. I have a fragment that is testing observing LiveData, for simplicity I’m showing the function with the observer. fun liveDataObserver() { viewModel.scoreLiveData.observe(viewLifecycleOwner, { Log.i("Practice", "New score is $it") } ) } I use Robolectric to launch the fragment in the test scope. @RunWith(RobolectricTestRunner::class) class ScoreKeeperFragmentTest ..

Read more

I am following this article and finding it hard to get my head around it. I am diving deeper into testing and need your help to understand it more. How to test my viewmodel? class SongViewModel (application: Application) : AndroidViewModel(application) { private val repository: SongRepository val allSongs: LiveData<List<LocalSong>> init { val songsDao = SongRoomDatabase.getDatabase(application, viewModelScope).localSongDao() ..

Read more

In the following android test fragment test class, https://github.com/TheReprator/UITestLearning/blob/master/app/src/androidTest/kotlin/com/example/uitest/FragmentDataBindingBTest.kt Only testDataBindingObserverValue() passed and other 2 fail(testDataBindingBDefaultValue(), testDataBindingBDefaultValue_updatedValue()), Ideally i am expecting, that i should be able to receive the initial value and then the new updated value, so i am atleast trying to make testDataBindingBDefaultValue_updatedValue(), test case to pass. My github Repo is as follows, ..

Read more

I am using Espresso for Instrumented Test on a Fragment but got this error on the Stack Trace. The error being caused by a missing class as shown below: Caused by: java.lang.ClassNotFoundException: Didn’t find class "org.hamcrest.Matchers" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/system/framework/android.test.base.jar", zip file "/data/app/~~vnZzxGNKnS4V6YkEf4falA==/com.example.android.architecture.blueprints.reactive.test-K_x0_yJ0hJeDHaJkDmHXRw==/base.apk", zip file "/data/app/~~oeYx2MgTcILbk-vq_WPx1A==/com.example.android.architecture.blueprints.reactive-0wMHYEe95hx_1cnbdAoZAw==/base.apk"],nativeLibraryDirectories It first ..

Read more

I am testing a basic app using Espresso. The mainActivity has an edit text and a voice Input button. The XML file is as follows: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.constraintlayout.widget.Group android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/textMessage" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:ems="10" android:hint="@string/edit_message" android:inputType="textPersonName" app:layout_constraintEnd_toStartOf="@+id/buttonSend" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button ..

Read more

Working on Android Instrumented Test to launch and test this fragment But I am running to this nasty error. java.lang.ClassCastException: androidx.test.runner.AndroidJUnitRunner cannot be cast to org.robolectric.android.fakes.RoboMonitoringInstrumentation at org.robolectric.android.internal.LocalActivityInvoker.getInstrumentation(LocalActivityInvoker.java:153) at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40) at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265) at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:226) at androidx.fragment.app.testing.FragmentScenario$Companion.internalLaunch$fragment_testing_release(FragmentScenario.kt:588) at androidx.fragment.app.testing.FragmentScenario$Companion.launchInContainer(FragmentScenario.kt:559) at com.example.android.architecture.blueprints.todoapp.taskdetail.TaskDetailFragmentTest.activeTasks_displayedInUi(TaskDetailFragmentTest.kt:35)Tests ran to completion. These are the dependencies that I’m using Have been looking for an ..

Read more

How do you create a Test that confirms a Toast is displayed in a fragment. Here are the bones of my test @RunWith(AndroidJUnit4:class) class MyFragmentTest() { val displayToast = R.id.btn_toast @Before() fun setup() { launchFragmentInContainer<MyFragment>(null, R.style.My_Theme) } @Test fun displayToast() { onView(withId(displayToast).perform(click()) // Insert different ideas here to display toast. } } Idea one onView(WithText("My ..

Read more