diff --git a/app/src/test/java/com/abhishek/pathak/kotlin/android/githubcompose/data/GithubApiTest.kt b/app/src/test/java/com/abhishek/pathak/kotlin/android/githubcompose/data/GithubApiTest.kt index 621933e..381a159 100644 --- a/app/src/test/java/com/abhishek/pathak/kotlin/android/githubcompose/data/GithubApiTest.kt +++ b/app/src/test/java/com/abhishek/pathak/kotlin/android/githubcompose/data/GithubApiTest.kt @@ -8,10 +8,12 @@ import okhttp3.OkHttpClient import okhttp3.mockwebserver.MockWebServer import org.junit.After import org.junit.Test +import retrofit2.HttpException import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import java.util.concurrent.TimeUnit import kotlin.test.assertEquals +import kotlin.test.assertFailsWith class GithubApiTest { @@ -68,6 +70,18 @@ class GithubApiTest { assertEquals("/users?since=51234842", request.path) } + @Test + fun `Given 404 response when fetching users the throws HTTPException`(){ + mockWebService.enqueueResponse( + fileName = "not-found.json", + code = 404 + ) + val exception = assertFailsWith { + runBlocking { api.getUsers() } + } + assertEquals(404,exception.code()) + } + @Test fun `Given 200 response When fetching user Then returns user correctly`() { // Given @@ -146,4 +160,18 @@ class GithubApiTest { assertEquals(expected, actual) assertEquals("/users/$userId/repos", request.path) } -} \ No newline at end of file + + @Test + fun `Given 404 When fetching user Then throws HttpException`(){ + val userId = "nonexistentuser" + mockWebService.enqueueResponse( + fileName = "not-found.json", + code = 404 + ) + val exception = assertFailsWith { + runBlocking { api.getUser(userId) } + } + assertEquals(404,exception.code()) + assertEquals("/users/$userId", mockWebService.takeRequest().path) + } +}