Skip to content

Instantly share code, notes, and snippets.

@anibalbastiass
Last active January 3, 2020 19:26
Show Gist options
  • Save anibalbastiass/250bdd8cd7cdc047891cca2f2d7e359f to your computer and use it in GitHub Desktop.
Save anibalbastiass/250bdd8cd7cdc047891cca2f2d7e359f to your computer and use it in GitHub Desktop.
CurrenciesRepositoryImplTest.kt
@ExperimentalCoroutinesApi
@RunWith(JUnit4::class)
class CurrenciesRepositoryImplTest {
private val restApi = mock<ForeignExchangeApiService>()
private val mapper = mock<CurrenciesUiMapper>()
private val testDispatcher = TestCoroutineDispatcher()
private val currenciesRemoteImpl = CurrenciesRepositoryImpl(restApi, mapper)
private val managedCoroutineScope: ManagedCoroutineScope =
TestScope(
testDispatcher
)
@Before
fun setup() {
Dispatchers.setMain(testDispatcher)
}
@Test
fun `given RemoteCurrency, when GetLatestCurrencies, then assert not null`() {
stubRestApiGetLatestCurrencies(makeRemoteCurrencies())
managedCoroutineScope.launch {
withContext(testDispatcher) {
val response = currenciesRemoteImpl.getLatestCurrencies("HRK")
Assert.assertNotNull(response)
}
}
}
@Test
fun `given RemoteCurrency, when GetLatestCurrencies, then returns data`() {
val remoteCurrencies = makeRemoteCurrencies()
stubRestApiGetLatestCurrencies(remoteCurrencies)
managedCoroutineScope.launch {
withContext(testDispatcher) {
val response = currenciesRemoteImpl.getLatestCurrencies("HRK")
Assert.assertEquals(response, remoteCurrencies)
}
}
}
private fun stubRestApiGetLatestCurrencies(currencies: RemoteCurrencies) {
whenever(restApi.getLatestCurrencies("HRK")).thenReturn(CallFake.buildSuccess(currencies))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment