Reputation: 478
I am trying to write a unit test for my Fragment class. Is it possible to instantiate "System Under Test" this way?
val sut = MyFragment()
I understand there are several methods for writing UI tests for testing fragments, but I want to instantiate my Fragment class where I can unit test methods within the fragment.
such as ,
@Test
fun testComponents() {
val bundle = Bundle()
val context = sut.requireContext()
sut.checkPwdEditTextIsEmpty(text = "")
}
How can I do this in Kotlin??
Also, how can I write init() and tearDown() in Kotlin test?
Upvotes: 0
Views: 165
Reputation: 199795
Methods like requireContext()
and accessing the Views of a Fragment require the Fragment to be added to a FragmentManager. As per the Test your app's fragments documentation, you can use FragmentScenario
to write exactly those types of tests:
@Test
fun testComponents() {
val scenario = launchFragmentInContainer<MyFragment>()
scenario.onFragment { sut ->
val context = sut.requireContext()
sut.checkPwdEditTextIsEmpty(text = "")
}
}
Upvotes: 3