Skip to content

Commit 9108183

Browse files
nnhai1991maibin
authored andcommitted
BAEL-2147 Kotlin Data Class from Json using GSON (eugenp#5220)
* BAEL-1846: Java Image to Base64 String * Move from using main method to Junit test * Update to use environment variables for testing * reformat and add test file * spring boot jsp security taglibs * add more test * add more test * refactor spring config * refactor spring config * Update README.md * fi alignment * fix requested comments * additional tests and content * additional tests and content * update examples * Delete Readme file * edit form example * adding example for spring boot security tag libs * Remove old tag libs module * BAEL-2147 Add GsonTest * BAEL-2147 Remove unused import
1 parent 41b2495 commit 9108183

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)