Created
October 13, 2021 16:06
-
-
Save wangerekaharun/8812ac0214824d9cb149a9e83ee23089 to your computer and use it in GitHub Desktop.
Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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