From f597580773fe46968b8c48d378fa309abb95a54c Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 20 Dec 2023 22:19:16 +0800 Subject: [PATCH] Tweak test --- .../test/java/retrofit2/KotlinSuspendTest.kt | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/retrofit/kotlin-test/src/test/java/retrofit2/KotlinSuspendTest.kt b/retrofit/kotlin-test/src/test/java/retrofit2/KotlinSuspendTest.kt index 2b16aeac0d..bdc72c3789 100644 --- a/retrofit/kotlin-test/src/test/java/retrofit2/KotlinSuspendTest.kt +++ b/retrofit/kotlin-test/src/test/java/retrofit2/KotlinSuspendTest.kt @@ -51,6 +51,7 @@ class KotlinSuspendTest { @GET("/") suspend fun unit() @HEAD("/") suspend fun headUnit() @GET("user") suspend fun getUser(): Result + @HEAD("user") suspend fun headUser(): Result @GET("/{a}/{b}/{c}") suspend fun params( @@ -378,7 +379,7 @@ class KotlinSuspendTest { } } - @Test fun testSuccessfulResponse() { + @Test fun returnResultType() { val responseBody = """ { "id": 1, @@ -386,19 +387,35 @@ class KotlinSuspendTest { "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")