Skip to content

Instantly share code, notes, and snippets.

@DHosseiny
Created September 24, 2019 17:37
Show Gist options
  • Save DHosseiny/ae68fb0cbbe45cc3f8ef85c8fdfa3ccc to your computer and use it in GitHub Desktop.
Save DHosseiny/ae68fb0cbbe45cc3f8ef85c8fdfa3ccc to your computer and use it in GitHub Desktop.
class PreferenceRepositoryTest {
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
@Mock
private lateinit var connectionApiServices: ConnectionApiServices
private lateinit var preferenceRepository: PrefrenceRepository
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
preferenceRepository = PrefrenceRepository(connectionApiServices)
}
@Test
fun `checkConnection on start set liveData to LOADING`() {
whenever(connectionApiServices.checkConnection()).thenReturn(mock())
val liveData = preferenceRepository.checkConnection()
assertEquals(liveData.value, Status.LOADING)
}
@Test
fun `checkConnection successful response set liveData to SUCCESS`() {
whenever(connectionApiServices.checkConnection()).thenReturn(Calls.response("TestResponse"))
val liveData = preferenceRepository.checkConnection()
assertEquals(liveData.value, Status.SUCCESS)
}
@Test
fun `checkConnection failure set liveData to ERROR`() {
whenever(connectionApiServices.checkConnection()).thenReturn(Calls.failure(IOException()))
val liveData = preferenceRepository.checkConnection()
assertEquals(liveData.value, Status.ERROR)
}
}
@DHosseiny
Copy link
Author

Retrofit mock + nhrmann/mockito kotlin(just a wrapper around mockito for kotlin)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment