How can we pass parameters to constructors in a Fragment while using them in Espresso Test Cases

213 views Asked by At

I am trying to launch a fragment with object as a parameter in my Espresso test case and I am unable to do so.

val homeFragment= launchFragmentInContainer(themeResId = R.style.AppTheme_NoActionBar)

But I want to send an object to the HomeFragment class.

For eg: val homeType:HomeType

I want to pass the homeType object to the below line of code, need help with how to do it. val homeFragment= launchFragmentInContainer(themeResId = R.style.AppTheme_NoActionBar)

I have looked into documentation and stackoverflow link(added below) and I am not able to get the desired solution.

https://developer.android.com/guide/fragments/test

Best practice for instantiating a new Android Fragment

Please help me with this issue.

1

There are 1 answers

2
pbl9 On

if you look at the launchFragmentInContainer function documentation, you can see that have parameter fragmentArgs of type Bundle. You can use it to pass arguments to fragment. I don't know how exactly your class HomeType looks like but remember that within Bundle you can pass only custom classes that implements java.io.Serializable or Parcelable. https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)