Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeremyrempel/e869177f0ac462e098f4d910d9829334 to your computer and use it in GitHub Desktop.
Save jeremyrempel/e869177f0ac462e098f4d910d9829334 to your computer and use it in GitHub Desktop.
@RunWith(AndroidJUnit4::class)
class MainFragmentTest {
@MockK
private lateinit var fakeViewModel: MainFragmentViewModel
@Before
fun setUp() = MockKAnnotations.init(this)
@Test
fun `testFragmentUpdateData`() {
// mock the viewmodel behavior
every { fakeViewModel.isLoading() } returns MutableLiveData(false)
every { fakeViewModel.getData() } returns MutableLiveData("Hello World from Mock")
// use androidx.test to launch fragment in isolation and inject in mocked ViewModel
launchFragmentInContainer<MainFragment>(
Bundle.EMPTY,
R.style.FragmentScenarioEmptyFragmentActivityTheme,
FragFactoryFake(fakeViewModel)
)
// verify results
onView(withId(R.id.loadingView))
.check(matches(withEffectiveVisibility(Visibility.GONE)))
onView(withId(R.id.textView))
.check(matches(withText("Hello World from Mock")))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment