Skip to content

Commit

Permalink
Tweak test
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Dec 20, 2023
1 parent 4805e0c commit f597580
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions retrofit/kotlin-test/src/test/java/retrofit2/KotlinSuspendTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class KotlinSuspendTest {
@GET("/") suspend fun unit()
@HEAD("/") suspend fun headUnit()
@GET("user") suspend fun getUser(): Result<User>
@HEAD("user") suspend fun headUser(): Result<Unit>

@GET("/{a}/{b}/{c}")
suspend fun params(
Expand Down Expand Up @@ -378,27 +379,43 @@ class KotlinSuspendTest {
}
}

@Test fun testSuccessfulResponse() {
@Test fun returnResultType() {
val responseBody = """
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com"
}
""".trimIndent()
server.enqueue(MockResponse().setResponseCode(200).setBody(responseBody))
val retrofit = Retrofit.Builder()
.baseUrl(server.url("/"))
.addCallAdapterFactory(ResultCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(Service::class.java)

// Successful response with body.
runBlocking {
server.enqueue(MockResponse().setResponseCode(200).setBody(responseBody))
val result = service.getUser()
assertThat(result.getOrThrow().id).isEqualTo(1)
assertThat(result.getOrThrow().name).isEqualTo("John Doe")
assertThat(result.getOrThrow().email).isEqualTo("john.doe@example.com")
}
// Successful response without body.
runBlocking {
server.enqueue(MockResponse())
val result = service.headUser()
assertThat(result.isSuccess).isTrue()
assertThat(result.getOrThrow()).isEqualTo(Unit)
}
// Error response without body.
runBlocking {
server.enqueue(MockResponse().setResponseCode(400))
val result = service.getUser()
assertThat(result.isFailure).isTrue()
assertThat(result.exceptionOrNull()).isInstanceOf(HttpException::class.java)
}
}

@Suppress("EXPERIMENTAL_OVERRIDE")
Expand Down

0 comments on commit f597580

Please sign in to comment.