BAEL-2147 Add GsonTest

This commit is contained in:
nnhai1991@gmail.com 2018-09-10 19:05:14 +08:00
parent d37c9e8045
commit 2ee3d2ffd9
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.baeldung.kotlin.gson
import com.baeldung.datetime.UsePeriod
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import java.time.LocalDate
import java.time.Period
import org.junit.Assert
import org.junit.Test
class GsonUnitTest {
var gson = Gson()
@Test
fun givenObject_thenGetJSONString() {
var jsonString = gson.toJson(TestModel(1,"Test"))
Assert.assertEquals(jsonString, "{\"id\":1,\"description\":\"Test\"}")
}
@Test
fun givenJSONString_thenGetObject() {
var jsonString = "{\"id\":1,\"description\":\"Test\"}";
var testModel = gson.fromJson(jsonString, TestModel::class.java)
Assert.assertEquals(testModel.id, 1)
Assert.assertEquals(testModel.description, "Test")
}
data class TestModel(
val id: Int,
val description: String
)
}