Skip to content

Commit

Permalink
Merge pull request #10 from myofficework000/failure-test-cases
Browse files Browse the repository at this point in the history
Added failure test cases for fetching user
  • Loading branch information
myofficework000 authored Jun 29, 2024
2 parents 1f006e4 + 81db22f commit c07b709
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<HttpException> {
runBlocking { api.getUsers() }
}
assertEquals(404,exception.code())
}

@Test
fun `Given 200 response When fetching user Then returns user correctly`() {
// Given
Expand Down Expand Up @@ -146,4 +160,18 @@ class GithubApiTest {
assertEquals(expected, actual)
assertEquals("/users/$userId/repos", request.path)
}
}

@Test
fun `Given 404 When fetching user Then throws HttpException`(){
val userId = "nonexistentuser"
mockWebService.enqueueResponse(
fileName = "not-found.json",
code = 404
)
val exception = assertFailsWith<HttpException> {
runBlocking { api.getUser(userId) }
}
assertEquals(404,exception.code())
assertEquals("/users/$userId", mockWebService.takeRequest().path)
}
}

0 comments on commit c07b709

Please sign in to comment.