Skip to content

Instantly share code, notes, and snippets.

@catalinghita8
Last active August 10, 2020 16:23
Show Gist options
  • Select an option

  • Save catalinghita8/c46b3d2b0ddc31109f40c5d18cca131a to your computer and use it in GitHub Desktop.

Select an option

Save catalinghita8/c46b3d2b0ddc31109f40c5d18cca131a to your computer and use it in GitHub Desktop.
class DataViewModelTest {
@Mock
lateinit var mockedDataSource: DataSource // Our mocked dependency
lateinit var dataViewModel: DataViewModel
@Before
fun setup() {
dataViewModel = DataViewModel(mockedDataSource) // This will now work!
}
fun testCorrectCall() {
// Let's first tell Mockito what our mocked dependency should return when queried
Mockito.`when`(mockedDataSource.getCachedData()).thenReturn("Mocked json value")
// Let's trigger the action on the ViewModel
dataViewModel.getCachedData()
// And finally let's test that our ViewModel called the right methods on its dependency
Mockito.verify(mockedDataSource).getCachedData()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment