I implemented a single activity multi module app. The fragment in the module uses the activity?.application as BaseApplication
to get the dao
for the fragment view model. Now, writing the espresso test, calling
launchFragmentInContainer<AddTripFragment>(themeResId = R.style.Theme)
it's calling a
java.lang.ClassCastException: android.app.Application cannot be cast to com.example.core.ui.BaseApplication
Is there a way to provide some type of application for the test case?
class ModuleFragment : Fragment() {
private val viewModel: ModuleViewModel by activityViewModels {
ModuleViewModelFactory(((activity?.application as BaseApplication).database).dao())
}
}
@RunWith(AndroidJUnit4::class)
class ModuleFragmentTest {
private lateinit var scenario: FragmentScenario<ModuleFragment>
@Before
fun setup() {
scenario = launchFragmentInContainer(themeResId = R.style)
}
}
I've tried to declare the app's module main activity test rule. However, that doesn't take me too far and is also leading to circular dependency.
val activityRule = ActivityTestRule(MainActivity::class.java)