1
+ package com.baeldung.retrofit
2
+
3
+ import com.fasterxml.jackson.databind.ObjectMapper
4
+ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
5
+ import com.github.tomakehurst.wiremock.client.WireMock
6
+ import com.github.tomakehurst.wiremock.junit.WireMockRule
7
+ import com.google.gson.Gson
8
+ import com.google.gson.GsonBuilder
9
+ import com.squareup.moshi.Moshi
10
+ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
11
+ import kotlinx.coroutines.runBlocking
12
+ import org.junit.Rule
13
+ import org.junit.jupiter.api.AfterEach
14
+ import org.junit.jupiter.api.Assertions.assertEquals
15
+ import org.junit.jupiter.api.BeforeEach
16
+ import org.junit.jupiter.api.Test
17
+ import retrofit2.Retrofit
18
+ import retrofit2.converter.gson.GsonConverterFactory
19
+ import retrofit2.converter.jackson.JacksonConverterFactory
20
+ import retrofit2.converter.moshi.MoshiConverterFactory
21
+ import retrofit2.http.Body
22
+ import retrofit2.http.GET
23
+ import retrofit2.http.POST
24
+ import retrofit2.http.Path
25
+
26
+ enum class OrderStatus { PENDING , COMPLETED , CANCELLED }
27
+
28
+ data class Order (val id : String , val status : OrderStatus )
29
+
30
+ interface OrderService {
31
+
32
+ @GET(" orders/{id}" )
33
+ suspend fun getOrder (@Path(" id" ) orderId : String ): Order
34
+
35
+ @POST(" orders" )
36
+ suspend fun createOrder (@Body order : Order ): Order
37
+ }
38
+
39
+ val gson: Gson = GsonBuilder ().create()
40
+ val gsonRetrofit: Retrofit = Retrofit .Builder ().baseUrl(" http://localhost:8080" ).addConverterFactory(
41
+ GsonConverterFactory .create(gson)
42
+ ).build()
43
+
44
+ val moshi: Moshi = Moshi .Builder ().add(KotlinJsonAdapterFactory ()).build()
45
+ val moshiRetrofit: Retrofit = Retrofit .Builder ().baseUrl(" http://localhost:8080" ).addConverterFactory(
46
+ MoshiConverterFactory .create(moshi)
47
+ ).build()
48
+
49
+ val objectMapper: ObjectMapper = jacksonObjectMapper()
50
+ val jacksonRetrofit: Retrofit = Retrofit .Builder ().baseUrl(" http://localhost:8080" ).addConverterFactory(
51
+ JacksonConverterFactory .create(objectMapper)
52
+ ).build()
53
+
54
+ class EnumSerializationUnitTest {
55
+ @Rule
56
+ @JvmField
57
+ val wireMockRule = WireMockRule (8080 )
58
+
59
+ @BeforeEach
60
+ fun setup () {
61
+ if (wireMockRule.isRunning) return
62
+ wireMockRule.start()
63
+ }
64
+
65
+ @AfterEach
66
+ fun tearDown () {
67
+ wireMockRule.stop()
68
+ }
69
+
70
+ @Test
71
+ fun `test Gson default serialization` () = runBlocking {
72
+ val service = gsonRetrofit.create(OrderService ::class .java)
73
+ val order = Order (" 1" , OrderStatus .PENDING )
74
+ wireMockRule.stubFor(
75
+ WireMock .post(WireMock .urlEqualTo(" /orders" ))
76
+ .willReturn(WireMock .aResponse().withBody(""" {"id":"1","status":"PENDING"}""" ).withStatus(200 ))
77
+ )
78
+ val response = service.createOrder(order)
79
+ assertEquals(order, response)
80
+ }
81
+
82
+ @Test
83
+ fun `test Moshi default serialization` () = runBlocking {
84
+ val service = moshiRetrofit.create(OrderService ::class .java)
85
+ val order = Order (" 1" , OrderStatus .PENDING )
86
+ wireMockRule.stubFor(
87
+ WireMock .post(WireMock .urlEqualTo(" /orders" ))
88
+ .willReturn(WireMock .aResponse().withBody(""" {"id":"1","status":"PENDING"}""" ).withStatus(200 ))
89
+ )
90
+ val response = service.createOrder(order)
91
+ assertEquals(order, response)
92
+ }
93
+
94
+ @Test
95
+ fun `test Jackson default serialization` () = runBlocking {
96
+ val service = jacksonRetrofit.create(OrderService ::class .java)
97
+ val order = Order (" 1" , OrderStatus .PENDING )
98
+ wireMockRule.stubFor(
99
+ WireMock .post(WireMock .urlEqualTo(" /orders" ))
100
+ .willReturn(WireMock .aResponse().withBody(""" {"id":"1","status":"PENDING"}""" ).withStatus(200 ))
101
+ )
102
+ val response = service.createOrder(order)
103
+ assertEquals(order, response)
104
+ }
105
+ }
0 commit comments