8
8
9
9
import static junit .framework .TestCase .*;
10
10
11
- public class UnitTest {
12
- @ Test public void toJsonAllPrimitives () {
11
+ public class PrimitiveValuesUnitTest {
12
+ @ Test public void whenSerializingToJSON_thenShouldCreateJSON () {
13
13
PrimitiveBundle primitiveBundle = new PrimitiveBundle ();
14
14
15
15
// @formatter:off
@@ -32,7 +32,8 @@ public class UnitTest {
32
32
assertEquals (expected , gson .toJson (primitiveBundle ));
33
33
}
34
34
35
- @ Test (expected = IllegalArgumentException .class ) public void toJsonInfinity () {
35
+ @ Test (expected = IllegalArgumentException .class ) public void
36
+ whenSerializingInfinity_thenShouldRaiseAnException () {
36
37
InfinityValuesExample model = new InfinityValuesExample ();
37
38
model .negativeInfinity = Float .NEGATIVE_INFINITY ;
38
39
model .positiveInfinity = Float .POSITIVE_INFINITY ;
@@ -42,15 +43,16 @@ public class UnitTest {
42
43
gson .toJson (model );
43
44
}
44
45
45
- @ Test (expected = IllegalArgumentException .class ) public void toJsonNaN () {
46
+ @ Test (expected = IllegalArgumentException .class ) public void
47
+ whenSerializingNaN_thenShouldRaiseAnException () {
46
48
FloatExample model = new FloatExample ();
47
49
model .value = Float .NaN ;
48
50
49
51
Gson gson = new Gson ();
50
52
gson .toJson (model );
51
53
}
52
54
53
- @ Test public void fromJsonAllPrimitives () {
55
+ @ Test public void whenDeserializingFromJSON_thenShouldParseTheValueInTheString () {
54
56
String json = "{\" byteValue\" : 17, \" shortValue\" : 3, \" intValue\" : 3, "
55
57
+ "\" longValue\" : 3, \" floatValue\" : 3.5" + ", \" doubleValue\" : 3.5"
56
58
+ ", \" booleanValue\" : true, \" charValue\" : \" a\" }" ;
@@ -70,30 +72,30 @@ public class UnitTest {
70
72
// @formatter:on
71
73
}
72
74
73
- @ Test public void fromJsonPrecissionMismatch () {
75
+ @ Test public void whenDeserializingHighPrecissionNumberIntoFloat_thenShouldPerformRounding () {
74
76
String json = "{\" value\" : 12.123425589123456}" ;
75
77
Gson gson = new Gson ();
76
78
FloatExample model = gson .fromJson (json , FloatExample .class );
77
79
assertEquals (12.123426f , model .value , 0.000001 );
78
80
}
79
81
80
- @ Test public void fromJsonPrecissionMismatchForDouble () {
82
+ @ Test public void whenDeserializingHighPrecissiongNumberIntoDouble_thenShouldPerformRounding () {
81
83
String json = "{\" value\" : 12.123425589123556}" ;
82
84
Gson gson = new Gson ();
83
85
DoubleExample model = gson .fromJson (json , DoubleExample .class );
84
86
assertEquals (12.123425589124f , model .value , 0.000001 );
85
87
}
86
88
87
89
88
- @ Test public void fromJsonOverflow () {
90
+ @ Test public void whenDeserializingValueThatOverflows_thenShouldOverflowSilently () {
89
91
Gson gson = new Gson ();
90
92
String json = "{\" value\" : \" 300\" }" ;
91
93
ByteExample model = gson .fromJson (json , ByteExample .class );
92
94
93
95
assertEquals (44 , model .value );
94
96
}
95
97
96
- @ Test public void fromJsonRealToByte () {
98
+ @ Test public void whenDeserializingRealIntoByte_thenShouldRaiseAnException () {
97
99
Gson gson = new Gson ();
98
100
String json = "{\" value\" : 2.3}" ;
99
101
try {
@@ -107,7 +109,7 @@ public class UnitTest {
107
109
fail ();
108
110
}
109
111
110
- @ Test public void fromJsonRealToLong () {
112
+ @ Test public void whenDeserializingRealIntoLong_thenShouldRaiseAnException () {
111
113
Gson gson = new Gson ();
112
114
String json = "{\" value\" : 2.3}" ;
113
115
try {
@@ -121,22 +123,22 @@ public class UnitTest {
121
123
fail ();
122
124
}
123
125
124
- @ Test public void fromJsonRealToLongEndingIn0 () {
126
+ @ Test public void whenDeserializingRealWhoseDecimalPartIs0_thenShouldParseItCorrectly () {
125
127
Gson gson = new Gson ();
126
128
String json = "{\" value\" : 2.0}" ;
127
129
LongExample model = gson .fromJson (json , LongExample .class );
128
130
assertEquals (2 , model .value );
129
131
}
130
132
131
- @ Test public void fromJsonUnicodeChar () {
133
+ @ Test public void whenDeserializingUnicodeChar_thenShouldParseItCorrectly () {
132
134
Gson gson = new Gson ();
133
135
String json = "{\" value\" : \" \\ u00AE\" }" ;
134
136
CharExample model = gson .fromJson (json , CharExample .class );
135
137
136
138
assertEquals ('\u00AE' , model .value );
137
139
}
138
140
139
- @ Test public void fromJsonNull () {
141
+ @ Test public void whenDeserializingNullValues_thenShouldIgnoreThoseFields () {
140
142
Gson gson = new Gson ();
141
143
// @formatter:off
142
144
String json = "{\" byteValue\" : null, \" shortValue\" : null, "
@@ -154,7 +156,8 @@ public class UnitTest {
154
156
assertEquals (1 , model .doubleValue , 0.0001 );
155
157
}
156
158
157
- @ Test (expected = JsonSyntaxException .class ) public void fromJsonEmptyString () {
159
+ @ Test (expected = JsonSyntaxException .class ) public void
160
+ whenDeserializingTheEmptyString_thenShouldRaiseAnException () {
158
161
Gson gson = new Gson ();
159
162
// @formatter:off
160
163
String json = "{\" byteValue\" : \" \" , \" shortValue\" : \" \" , "
@@ -164,7 +167,7 @@ public class UnitTest {
164
167
gson .fromJson (json , PrimitiveBundleInitialized .class );
165
168
}
166
169
167
- @ Test public void fromJsonEmptyStringToChar () {
170
+ @ Test public void whenDeserializingTheEmptyStringIntoChar_thenShouldHaveTheEmtpyChar () {
168
171
Gson gson = new Gson ();
169
172
// @formatter:off
170
173
String json = "{\" charValue\" : \" \" }" ;
@@ -174,7 +177,7 @@ public class UnitTest {
174
177
assertEquals (Character .MIN_VALUE , model .value );
175
178
}
176
179
177
- @ Test public void fromJsonValidValueWithinString () {
180
+ @ Test public void whenDeserializingValidValueAppearingInAString_thenShouldParseTheValue () {
178
181
Gson gson = new Gson ();
179
182
// @formatter:off
180
183
String json = "{\" byteValue\" : \" 15\" , \" shortValue\" : \" 15\" , "
@@ -192,7 +195,7 @@ public class UnitTest {
192
195
assertEquals (15 , model .doubleValue , 0.0001 );
193
196
}
194
197
195
- @ Test public void fromJsonBooleanFrom2ValueInteger () {
198
+ @ Test public void whenDeserializingABooleanFrom0Or1Integer_thenShouldRaiseAnException () {
196
199
String json = "{\" value\" : 1}" ;
197
200
Gson gson = new Gson ();
198
201
@@ -207,7 +210,7 @@ public class UnitTest {
207
210
fail ();
208
211
}
209
212
210
- @ Test public void fromJsonBooleanFrom2ValueIntegerSolution () {
213
+ @ Test public void whenDeserializingWithCustomDeserializerABooleanFrom0Or1Integer_thenShouldWork () {
211
214
String json = "{\" value\" : 1}" ;
212
215
GsonBuilder builder = new GsonBuilder ();
213
216
builder .registerTypeAdapter (BooleanExample .class ,
@@ -220,25 +223,6 @@ public class UnitTest {
220
223
assertTrue (model .value );
221
224
}
222
225
223
- @ Test public void fromJsonBooleanFromYes () {
224
- String json = "{\" value\" : yes}" ;
225
- Gson gson = new Gson ();
226
-
227
- BooleanExample model = gson .fromJson (json , BooleanExample .class );
228
-
229
- // pay attention here that we are deserializing yes.
230
- assertFalse (model .value );
231
- }
232
-
233
- @ Test public void fromJsonBooleanFromInvalidValue () {
234
- String json = "{\" value\" : \" 15x\" }" ;
235
- Gson gson = new Gson ();
236
-
237
- BooleanExample model = gson .fromJson (json , BooleanExample .class );
238
-
239
- assertFalse (model .value );
240
- }
241
-
242
226
// @formatter:off
243
227
static class BooleanAs2ValueIntegerDeserializer implements JsonDeserializer <BooleanExample > {
244
228
@ Override public BooleanExample deserialize (
0 commit comments