Skip to content

Commit

Permalink
Remove gson from kotlin-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Jan 29, 2024
1 parent 8cc3c88 commit 1a65812
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
1 change: 0 additions & 1 deletion retrofit/kotlin-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ apply plugin: 'org.jetbrains.kotlin.jvm'
dependencies {
testImplementation projects.retrofit
testImplementation projects.retrofit.testHelpers
testImplementation projects.retrofitConverters.gson
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
Expand Down
26 changes: 7 additions & 19 deletions retrofit/kotlin-test/src/test/java/retrofit2/KotlinSuspendTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import org.junit.Assert.fail
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.helpers.ToStringConverterFactory
import retrofit2.http.GET
import retrofit2.http.HEAD
Expand All @@ -61,7 +60,7 @@ class KotlinSuspendTest {
suspend fun headUnit()

@GET("user")
suspend fun getUser(): Result<User>
suspend fun getString(): Result<String>

@HEAD("user")
suspend fun headUser(): Result<Unit>
Expand All @@ -77,8 +76,6 @@ class KotlinSuspendTest {
suspend fun bodyWithCallType(): Call<String>
}

data class User(val id: Int, val name: String, val email: String)

@Test fun body() {
val retrofit = Retrofit.Builder()
.baseUrl(server.url("/"))
Expand Down Expand Up @@ -399,27 +396,18 @@ class KotlinSuspendTest {
}

@Test fun returnResultType() = runBlocking {
val responseBody = """
{
"id": 1,
"name": "John Doe",
"email": "[email protected]"
}
""".trimIndent()
val retrofit = Retrofit.Builder()
.baseUrl(server.url("/"))
.addCallAdapterFactory(ResultCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ToStringConverterFactory())
.build()
val service = retrofit.create(Service::class.java)

// Successful response with body.
server.enqueue(MockResponse().setBody(responseBody))
service.getUser().let { result ->
server.enqueue(MockResponse().setBody("Hello World"))
service.getString().let { result ->
assertThat(result.isSuccess).isTrue()
assertThat(result.getOrThrow().id).isEqualTo(1)
assertThat(result.getOrThrow().name).isEqualTo("John Doe")
assertThat(result.getOrThrow().email).isEqualTo("[email protected]")
assertThat(result.getOrThrow()).isEqualTo("Hello World")
}

// Successful response without body.
Expand All @@ -431,7 +419,7 @@ class KotlinSuspendTest {

// Error response without body.
server.enqueue(MockResponse().setResponseCode(404))
service.getUser().let { result ->
service.getString().let { result ->
assertThat(result.isFailure).isTrue()
assertThat(result.exceptionOrNull()).let {
it.hasMessageThat().isEqualTo("HTTP 404 Client Error")
Expand All @@ -441,7 +429,7 @@ class KotlinSuspendTest {

// Network error.
server.shutdown()
service.getUser().let { result ->
service.getString().let { result ->
assertThat(result.isFailure).isTrue()
assertThat(result.exceptionOrNull()).isInstanceOf(IOException::class.java)
}
Expand Down

0 comments on commit 1a65812

Please sign in to comment.