Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created June 15, 2020 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdmunoz/6201566760272be884a3d6e65999b9e8 to your computer and use it in GitHub Desktop.
Save cdmunoz/6201566760272be884a3d6e65999b9e8 to your computer and use it in GitHub Desktop.
@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