Skip to content

Instantly share code, notes, and snippets.

@wangerekaharun
Created October 13, 2021 16:06
Show Gist options
  • Save wangerekaharun/8812ac0214824d9cb149a9e83ee23089 to your computer and use it in GitHub Desktop.
Save wangerekaharun/8812ac0214824d9cb149a9e83ee23089 to your computer and use it in GitHub Desktop.
Test
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28])
class ExchangeRatesRepositoryImplTest : KoinTest {
private val mockWebServer = MockWebServer()
lateinit var repo: ExchangeRateRepository
private val api: ExchangeRateAPI by inject()
@get:Rule
val koinTestRule = KoinTestRule.create {
printLogger(Level.DEBUG)
modules(dataModules + listOf(fakeContext))
}
@Before
fun setup() {
mockWebServer.start()
declare {
val gson = GsonBuilder()
.serializeNulls()
.create()
Retrofit.Builder()
.baseUrl(mockWebServer.url("/"))
.addConverterFactory(GsonConverterFactory.create(gson))
.client(get())
.build()
}
repo = ExchangeRatesRepositoryImpl(api)
}
@After
fun tearDown() {
mockWebServer.shutdown()
}
@Test
fun `test exchange rates are fetched successfully`() = runBlocking {
mockWebServer.enqueue(
MockResponse().setResponseCode(HttpURLConnection.HTTP_OK).setBody(getJson("exchangerates.json"))
)
val result: NetworkResult<ExchangeRateDomainModel> = repo.fetchExchangeRates()
MatcherAssert.assertThat(result, CoreMatchers.instanceOf(NetworkResult.Success::class.java))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment