@@ -7,17 +7,15 @@ import com.fasterxml.jackson.annotation.JsonProperty
77import com.fasterxml.jackson.databind.SerializationFeature
88import com.fasterxml.jackson.module.kotlin.*
99import com.fasterxml.jackson.module.kotlin.readValue
10- import org.hamcrest.CoreMatchers.equalTo
11- import org.hamcrest.MatcherAssert.assertThat
10+ import org.junit.jupiter.api.Assertions.assertEquals
1211import org.junit.jupiter.api.Test
1312import kotlin.properties.Delegates
1413import kotlin.test.assertNull
1514import kotlin.test.fail
1615
17-
1816private data class DataClassPerson (val name : String , val age : Int )
1917
20- class TestM11Changes {
18+ private class TestM11Changes {
2119 val mapper = jacksonObjectMapper().configure(SerializationFeature .INDENT_OUTPUT , false )
2220
2321 private class Class_With_One_Constructor (val name : String , val age : Int )
@@ -27,26 +25,24 @@ class TestM11Changes {
2725 val expectedPerson = Class_With_One_Constructor (" John Smith" , 30 )
2826
2927 val actualJson = mapper.writeValueAsString(expectedPerson)
30- val newPerson = mapper.readValue<Class_With_One_Constructor >(actualJson)
28+ val newPerson = mapper.readValue<Class_With_One_Constructor >(actualJson)
3129
32- assertThat(actualJson, equalTo(expectedJson) )
33- assertThat(newPerson .name, equalTo(expectedPerson .name) )
34- assertThat(newPerson .age, equalTo(expectedPerson .age) )
30+ assertEquals(expectedJson, actualJson )
31+ assertEquals(expectedPerson .name, newPerson .name)
32+ assertEquals(expectedPerson .age, newPerson .age)
3533 }
3634
3735 private data class Class_Data_Annotation_With_One_Constructor (val name : String , val age : Int )
3836
3937 @Test fun testDataClass_One_Constructor () {
40-
41-
4238 val expectedJson = """ {"name":"John Smith","age":30}"""
4339 val expectedPerson = Class_Data_Annotation_With_One_Constructor (" John Smith" , 30 )
4440
4541 val actualJson = mapper.writeValueAsString(expectedPerson)
46- val newPerson = mapper.readValue<Class_Data_Annotation_With_One_Constructor >(actualJson)
42+ val newPerson = mapper.readValue<Class_Data_Annotation_With_One_Constructor >(actualJson)
4743
48- assertThat(actualJson, equalTo(expectedJson) )
49- assertThat(newPerson, equalTo(expectedPerson) )
44+ assertEquals(expectedJson, actualJson )
45+ assertEquals(expectedPerson, newPerson )
5046 }
5147
5248 private data class Class_With_Init_Constructor (val name : String , val age : Int ) {
@@ -57,15 +53,14 @@ class TestM11Changes {
5753 }
5854
5955 @Test fun testDataClass_Init_Constructor () {
60-
6156 val expectedJson = """ {"name":"John Smith","age":30,"otherThing":"franky"}"""
6257 val expectedPerson = Class_With_Init_Constructor (" John Smith" , 30 )
6358
6459 val actualJson = mapper.writeValueAsString(expectedPerson)
65- val newPerson = mapper.readValue<Class_With_Init_Constructor >(actualJson)
60+ val newPerson = mapper.readValue<Class_With_Init_Constructor >(actualJson)
6661
67- assertThat(actualJson, equalTo(expectedJson) )
68- assertThat(newPerson, equalTo(expectedPerson) )
62+ assertEquals(expectedJson, actualJson )
63+ assertEquals(expectedPerson, newPerson )
6964 }
7065
7166 private data class Class_With_Init_Constructor_And_Ignored_Property (val name : String , val age : Int ) {
@@ -76,105 +71,99 @@ class TestM11Changes {
7671 }
7772
7873 @Test fun testDataClass_Init_Constructor_And_Ignored_Property () {
79-
8074 val expectedJson = """ {"name":"John Smith","age":30}"""
8175 val expectedPerson = Class_With_Init_Constructor_And_Ignored_Property (" John Smith" , 30 )
8276
8377 val actualJson = mapper.writeValueAsString(expectedPerson)
84- val newPerson = mapper.readValue<Class_With_Init_Constructor_And_Ignored_Property >(actualJson)
78+ val newPerson = mapper.readValue<Class_With_Init_Constructor_And_Ignored_Property >(actualJson)
8579
86- assertThat(actualJson, equalTo(expectedJson) )
87- assertThat(newPerson, equalTo(expectedPerson) )
80+ assertEquals(expectedJson, actualJson )
81+ assertEquals(expectedPerson, newPerson )
8882 }
8983
9084 private class Class_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter (val name : String , age : Int ) {
9185 val age: Int = age
9286 }
9387
9488 @Test fun testDataClass_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter () {
95-
9689 val expectedJson = """ {"name":"John Smith","age":30}"""
9790 val expectedPerson = Class_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter (" John Smith" , 30 )
9891
9992 val actualJson = mapper.writeValueAsString(expectedPerson)
100- val newPerson = mapper.readValue<Class_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter >(actualJson)
93+ val newPerson = mapper.readValue<Class_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter >(actualJson)
10194
102- assertThat(actualJson, equalTo(expectedJson) )
103- assertThat(newPerson .name, equalTo(expectedPerson .name) )
104- assertThat(newPerson .age, equalTo(expectedPerson .age) )
95+ assertEquals(expectedJson, actualJson )
96+ assertEquals(expectedPerson .name, newPerson .name)
97+ assertEquals(expectedPerson .age, newPerson .age)
10598 }
10699
107100 private class ClassFor_testDataClass_WithOnlySecondaryConstructor {
108101 val name: String
109102 val age: Int
110103 constructor (name: String , age: Int ) {
111- this .name = name
104+ this .name = name
112105 this .age = age
113106 }
114107 }
115108
116109 @Test fun testDataClass_WithOnlySecondaryConstructor () {
117-
118110 val expectedJson = """ {"name":"John Smith","age":30}"""
119111 val expectedPerson = ClassFor_testDataClass_WithOnlySecondaryConstructor (" John Smith" , 30 )
120112
121113 val actualJson = mapper.writeValueAsString(expectedPerson)
122- val newPerson = mapper.readValue<ClassFor_testDataClass_WithOnlySecondaryConstructor >(actualJson)
114+ val newPerson = mapper.readValue<ClassFor_testDataClass_WithOnlySecondaryConstructor >(actualJson)
123115
124- assertThat(actualJson, equalTo(expectedJson) )
125- assertThat(newPerson .name, equalTo(expectedPerson .name) )
126- assertThat(newPerson .age, equalTo(expectedPerson .age) )
116+ assertEquals(expectedJson, actualJson )
117+ assertEquals(expectedPerson .name, newPerson .name)
118+ assertEquals(expectedPerson .age, newPerson .age)
127119 }
128120
129-
130121 private class Class_WithPrimaryAndSecondaryConstructor (val name : String , val age : Int ) {
131122 constructor (nameAndAge: String ) : this (nameAndAge.substringBefore(' :' ), nameAndAge.substringAfter(' :' ).toInt()) {
132-
133123 }
134124 }
135125
136126 @Test fun testDataClass_WithPrimaryAndSecondaryConstructor () {
137-
138127 val expectedJson = """ {"name":"John Smith","age":30}"""
139128 val expectedPerson = Class_WithPrimaryAndSecondaryConstructor (" John Smith" , 30 )
140129
141130 val actualJson = mapper.writeValueAsString(expectedPerson)
142- val newPerson = mapper.readValue<Class_WithPrimaryAndSecondaryConstructor >(actualJson)
131+ val newPerson = mapper.readValue<Class_WithPrimaryAndSecondaryConstructor >(actualJson)
143132
144- assertThat(actualJson, equalTo(expectedJson) )
145- assertThat(newPerson .name, equalTo(expectedPerson .name) )
146- assertThat(newPerson .age, equalTo(expectedPerson .age) )
133+ assertEquals(expectedJson, actualJson )
134+ assertEquals(expectedPerson .name, newPerson .name)
135+ assertEquals(expectedPerson .age, newPerson .age)
147136 }
148137
149138 private class Class_WithPrimaryAndSecondaryConstructorAnnotated (name : String ) {
150139 val name: String = name
151140 var age: Int = 0
141+
152142 @JsonCreator constructor (name: String , age: Int ) : this (name) {
153143 this .age = age
154144 }
155145 }
156146
157147 @Test fun testDataClass_WithPrimaryAndSecondaryConstructorBothCouldBeUsedToDeserialize () {
158-
159148 val expectedJson = """ {"name":"John Smith","age":30}"""
160149 val expectedPerson = Class_WithPrimaryAndSecondaryConstructorAnnotated (" John Smith" , 30 )
161150
162151 val actualJson = mapper.writeValueAsString(expectedPerson)
163- val newPerson = mapper.readValue<Class_WithPrimaryAndSecondaryConstructorAnnotated >(actualJson)
152+ val newPerson = mapper.readValue<Class_WithPrimaryAndSecondaryConstructorAnnotated >(actualJson)
164153
165- assertThat(actualJson, equalTo(expectedJson) )
166- assertThat(newPerson .name, equalTo(expectedPerson .name) )
167- assertThat(newPerson .age, equalTo(expectedPerson .age) )
154+ assertEquals(expectedJson, actualJson )
155+ assertEquals(expectedPerson .name, newPerson .name)
156+ assertEquals(expectedPerson .age, newPerson .age)
168157
169158 val jsonWithNoAge = """ {"name":"John Smith"}"""
170- val personNoAge = mapper.readValue<Class_WithPrimaryAndSecondaryConstructorAnnotated >(jsonWithNoAge)
159+ val personNoAge = mapper.readValue<Class_WithPrimaryAndSecondaryConstructorAnnotated >(jsonWithNoAge)
171160
172- assertThat( personNoAge.age, equalTo( 0 ) )
173- assertThat(personNoAge.name, equalTo( " John Smith" ) )
161+ assertEquals( 0 , personNoAge.age)
162+ assertEquals( " John Smith" , personNoAge.name )
174163 }
175164
176165 @JsonInclude(JsonInclude .Include .NON_EMPTY )
177- class Class_WithPartialFieldsInConstructor (val name : String , @JsonProperty(" age" ) val years : Int ) {
166+ class Class_WithPartialFieldsInConstructor (val name : String , @JsonProperty(" age" ) val years : Int ) {
178167 @JsonProperty(" address" ) var primaryAddress: String = " "
179168 var phone: String by Delegates .notNull()
180169 }
@@ -187,24 +176,24 @@ class TestM11Changes {
187176 val actualJson = mapper.writeValueAsString(expectedPerson)
188177 val newPerson = mapper.readValue<Class_WithPartialFieldsInConstructor >(actualJson)
189178
190- assertThat(actualJson, equalTo(expectedJson) )
191- assertThat(newPerson .name, equalTo(expectedPerson .name) )
192- assertThat(newPerson .years, equalTo(expectedPerson .years) )
193- assertThat(newPerson .phone, equalTo(expectedPerson .phone) )
194- assertThat(newPerson .primaryAddress, equalTo(expectedPerson .primaryAddress) )
179+ assertEquals(expectedJson, actualJson )
180+ assertEquals(expectedPerson .name, newPerson .name)
181+ assertEquals(expectedPerson .years, newPerson .years)
182+ assertEquals(expectedPerson .phone, newPerson .phone)
183+ assertEquals(expectedPerson .primaryAddress, newPerson .primaryAddress)
195184
196185 val jsonWithNullPhone = """ {"name":"John Smith","age":30}"""
197186 val person = mapper.readValue<Class_WithPartialFieldsInConstructor >(jsonWithNullPhone)
198187
199188 try {
200189 person.phone
201190 fail(" While person can be deserialized without a phone, phone must be set before attempting to access it" )
202- } catch (e: IllegalStateException ) { // expected
191+ } catch (e: IllegalStateException ) { // expected
203192 }
204193 }
205194
206195 @Test fun testNullableType () {
207196 val newPerson = mapper.readValue<Class_WithPartialFieldsInConstructor ?>(" null" )
208197 assertNull(newPerson)
209198 }
210- }
199+ }
0 commit comments