Skip to content

Commit 81db22f

Browse files
author
nishanthRaju2001
committed
Added failure test cases for fetching user
1 parent 1f006e4 commit 81db22f

File tree

1 file changed

+29
-1
lines changed
  • app/src/test/java/com/abhishek/pathak/kotlin/android/githubcompose/data

1 file changed

+29
-1
lines changed

app/src/test/java/com/abhishek/pathak/kotlin/android/githubcompose/data/GithubApiTest.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import okhttp3.OkHttpClient
88
import okhttp3.mockwebserver.MockWebServer
99
import org.junit.After
1010
import org.junit.Test
11+
import retrofit2.HttpException
1112
import retrofit2.Retrofit
1213
import retrofit2.converter.gson.GsonConverterFactory
1314
import java.util.concurrent.TimeUnit
1415
import kotlin.test.assertEquals
16+
import kotlin.test.assertFailsWith
1517

1618
class GithubApiTest {
1719

@@ -68,6 +70,18 @@ class GithubApiTest {
6870
assertEquals("/users?since=51234842", request.path)
6971
}
7072

73+
@Test
74+
fun `Given 404 response when fetching users the throws HTTPException`(){
75+
mockWebService.enqueueResponse(
76+
fileName = "not-found.json",
77+
code = 404
78+
)
79+
val exception = assertFailsWith<HttpException> {
80+
runBlocking { api.getUsers() }
81+
}
82+
assertEquals(404,exception.code())
83+
}
84+
7185
@Test
7286
fun `Given 200 response When fetching user Then returns user correctly`() {
7387
// Given
@@ -146,4 +160,18 @@ class GithubApiTest {
146160
assertEquals(expected, actual)
147161
assertEquals("/users/$userId/repos", request.path)
148162
}
149-
}
163+
164+
@Test
165+
fun `Given 404 When fetching user Then throws HttpException`(){
166+
val userId = "nonexistentuser"
167+
mockWebService.enqueueResponse(
168+
fileName = "not-found.json",
169+
code = 404
170+
)
171+
val exception = assertFailsWith<HttpException> {
172+
runBlocking { api.getUser(userId) }
173+
}
174+
assertEquals(404,exception.code())
175+
assertEquals("/users/$userId", mockWebService.takeRequest().path)
176+
}
177+
}

0 commit comments

Comments
 (0)