From 2ee3d2ffd9c627f39e0cbcb0df0e5a23741a4be0 Mon Sep 17 00:00:00 2001 From: "nnhai1991@gmail.com" Date: Mon, 10 Sep 2018 19:05:14 +0800 Subject: [PATCH] BAEL-2147 Add GsonTest --- .../com/baeldung/kotlin/gson/GsonUnitTest.kt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/gson/GsonUnitTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/gson/GsonUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/gson/GsonUnitTest.kt new file mode 100644 index 0000000000..fce3225ef8 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/gson/GsonUnitTest.kt @@ -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 + ) +} \ No newline at end of file