Created
June 15, 2020 20:27
-
-
Save cdmunoz/6201566760272be884a3d6e65999b9e8 to your computer and use it in GitHub Desktop.
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(JUnit4::class) | |
class PhotosRepositoryTest : MockWebServerBaseTest() { | |
@get:Rule | |
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule() | |
private lateinit var photosRepository: PhotosRepository | |
private lateinit var apiService: ApiService | |
private val sol = anyInt() | |
private val page = anyInt() | |
override fun isMockServerEnabled() = true | |
@Before | |
fun start() { | |
apiService = provideTestApiService() | |
photosRepository = PhotosRepository(apiService) | |
} | |
@Test | |
fun `given response ok when fetching results then return a list with elements`() { | |
runBlocking { | |
mockHttpResponse("json/photos_response_one_item.json", HttpURLConnection.HTTP_OK) | |
val apiResponse = photosRepository.getPhotosFromApi(sol, page) | |
assertNotNull(apiResponse) | |
assertEquals(apiResponse.extractData?.size, 1) | |
} | |
} | |
@Test | |
fun `given response ok when fetching empty results then return an empty list`() { | |
runBlocking { | |
mockHttpResponse("json/photos_response_empty_list.json", HttpURLConnection.HTTP_OK) | |
val apiResponse = photosRepository.getPhotosFromApi(sol, page) | |
assertNotNull(apiResponse) | |
assertEquals(apiResponse.extractData?.size, 0) | |
} | |
} | |
@Test | |
fun `given response failure when fetching results then return exception`() { | |
runBlocking { | |
mockHttpResponse(PhotosRepository.GENERAL_ERROR_CODE) | |
val apiResponse = photosRepository.getPhotosFromApi(sol, page) | |
assertNotNull(apiResponse) | |
val expectedValue = Result.Error(Exception(SOMETHING_WRONG)) | |
assertEquals(expectedValue.exception.message, (apiResponse as Result.Error).exception.message) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment