BAEL-2147 Add GsonTest
This commit is contained in:
parent
d37c9e8045
commit
2ee3d2ffd9
|
@ -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
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue