Skip to content

Commit 2ee3d2f

Browse files
nnhai1991@gmail.comnnhai1991@gmail.com
authored andcommitted
BAEL-2147 Add GsonTest
1 parent d37c9e8 commit 2ee3d2f

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.kotlin.gson
2+
3+
import com.baeldung.datetime.UsePeriod
4+
import com.google.gson.Gson
5+
import com.google.gson.annotations.SerializedName
6+
import java.time.LocalDate
7+
import java.time.Period
8+
9+
import org.junit.Assert
10+
import org.junit.Test
11+
12+
class GsonUnitTest {
13+
14+
var gson = Gson()
15+
16+
@Test
17+
fun givenObject_thenGetJSONString() {
18+
var jsonString = gson.toJson(TestModel(1,"Test"))
19+
Assert.assertEquals(jsonString, "{\"id\":1,\"description\":\"Test\"}")
20+
}
21+
22+
@Test
23+
fun givenJSONString_thenGetObject() {
24+
var jsonString = "{\"id\":1,\"description\":\"Test\"}";
25+
var testModel = gson.fromJson(jsonString, TestModel::class.java)
26+
Assert.assertEquals(testModel.id, 1)
27+
Assert.assertEquals(testModel.description, "Test")
28+
}
29+
30+
data class TestModel(
31+
val id: Int,
32+
val description: String
33+
)
34+
}

0 commit comments

Comments
 (0)