Created
August 3, 2019 13:35
-
-
Save AungThiha/b2524f09639985b3ce9183479a1086f0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import io.reactivex.android.plugins.RxAndroidPlugins | |
| import io.reactivex.plugins.RxJavaPlugins | |
| import io.reactivex.schedulers.Schedulers | |
| import okhttp3.MediaType | |
| import okhttp3.ResponseBody | |
| import org.junit.Before | |
| import org.junit.Test | |
| import org.mockito.ArgumentMatchers.any | |
| import org.mockito.Mock | |
| import org.mockito.Mockito | |
| import org.mockito.MockitoAnnotations | |
| import retrofit2.HttpException | |
| import retrofit2.Response | |
| class PromotionRepositoryTest { | |
| @Mock | |
| private lateinit var promotionService: PromotionService | |
| private lateinit var promotionRepository: PromotionRepository | |
| @Before | |
| fun setUp() { | |
| RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() } | |
| RxJavaPlugins.setNewThreadSchedulerHandler { Schedulers.trampoline() } | |
| RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() } | |
| RxJavaPlugins.setSingleSchedulerHandler { Schedulers.trampoline() } | |
| RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() } | |
| MockitoAnnotations.initMocks(this) | |
| promotionRepository = PromotionRepository( | |
| promotionService | |
| ) | |
| } | |
| private fun <T> serverError500(): Response<T> { | |
| return Response.error<T>( | |
| 500, | |
| ResponseBody.create( | |
| MediaType.parse("application/json"), | |
| "{\"code\":500,\"message\":\"Internal Server Error\"}" | |
| ) | |
| ) | |
| } | |
| @Test | |
| fun testFetchAdsFail(){ | |
| Mockito.`when`(promotionService.fetchAds(any())) | |
| .thenReturn(Single.just(serverError500())) | |
| val receivedAds = promotionRepository.fetchAds(1) | |
| // it's important to note the response is already consumed by the repository class | |
| // so when we want to crosscheck the error, we need to build that again | |
| // that's why we call serverError500<List<Ad>>() again | |
| receivedAds.test() | |
| .assertError(HttpException(serverError500<List<Ad>>())) | |
| } | |
| @Test | |
| fun testFetchAdsSuccess(){ | |
| val ads = listOf( | |
| Ad(), | |
| Ad() | |
| ) | |
| Mockito.`when`(promotionService.fetchAds(any())) | |
| .thenReturn(Single.just(Response.success(ads))) | |
| val receivedAds = promotionRepository.fetchAds(1) | |
| receivedAds.test() | |
| .assertValue { it.get() == ads } | |
| } | |
| @Test | |
| fun testFetchAdsSuccessNull(){ | |
| Mockito.`when`(promotionService.fetchAds(any())) | |
| .thenReturn(Single.just(Response.success(null))) | |
| val receivedAds = promotionRepository.fetchAds(1) | |
| receivedAds.test() | |
| .assertValue { it.isEmpty } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment