88
99import static junit .framework .TestCase .*;
1010
11- public class UnitTest {
12- @ Test public void toJsonAllPrimitives () {
11+ public class PrimitiveValuesUnitTest {
12+ @ Test public void whenSerializingToJSON_thenShouldCreateJSON () {
1313 PrimitiveBundle primitiveBundle = new PrimitiveBundle ();
1414
1515 // @formatter:off
@@ -32,7 +32,8 @@ public class UnitTest {
3232 assertEquals (expected , gson .toJson (primitiveBundle ));
3333 }
3434
35- @ Test (expected = IllegalArgumentException .class ) public void toJsonInfinity () {
35+ @ Test (expected = IllegalArgumentException .class ) public void
36+ whenSerializingInfinity_thenShouldRaiseAnException () {
3637 InfinityValuesExample model = new InfinityValuesExample ();
3738 model .negativeInfinity = Float .NEGATIVE_INFINITY ;
3839 model .positiveInfinity = Float .POSITIVE_INFINITY ;
@@ -42,15 +43,16 @@ public class UnitTest {
4243 gson .toJson (model );
4344 }
4445
45- @ Test (expected = IllegalArgumentException .class ) public void toJsonNaN () {
46+ @ Test (expected = IllegalArgumentException .class ) public void
47+ whenSerializingNaN_thenShouldRaiseAnException () {
4648 FloatExample model = new FloatExample ();
4749 model .value = Float .NaN ;
4850
4951 Gson gson = new Gson ();
5052 gson .toJson (model );
5153 }
5254
53- @ Test public void fromJsonAllPrimitives () {
55+ @ Test public void whenDeserializingFromJSON_thenShouldParseTheValueInTheString () {
5456 String json = "{\" byteValue\" : 17, \" shortValue\" : 3, \" intValue\" : 3, "
5557 + "\" longValue\" : 3, \" floatValue\" : 3.5" + ", \" doubleValue\" : 3.5"
5658 + ", \" booleanValue\" : true, \" charValue\" : \" a\" }" ;
@@ -70,30 +72,30 @@ public class UnitTest {
7072 // @formatter:on
7173 }
7274
73- @ Test public void fromJsonPrecissionMismatch () {
75+ @ Test public void whenDeserializingHighPrecissionNumberIntoFloat_thenShouldPerformRounding () {
7476 String json = "{\" value\" : 12.123425589123456}" ;
7577 Gson gson = new Gson ();
7678 FloatExample model = gson .fromJson (json , FloatExample .class );
7779 assertEquals (12.123426f , model .value , 0.000001 );
7880 }
7981
80- @ Test public void fromJsonPrecissionMismatchForDouble () {
82+ @ Test public void whenDeserializingHighPrecissiongNumberIntoDouble_thenShouldPerformRounding () {
8183 String json = "{\" value\" : 12.123425589123556}" ;
8284 Gson gson = new Gson ();
8385 DoubleExample model = gson .fromJson (json , DoubleExample .class );
8486 assertEquals (12.123425589124f , model .value , 0.000001 );
8587 }
8688
8789
88- @ Test public void fromJsonOverflow () {
90+ @ Test public void whenDeserializingValueThatOverflows_thenShouldOverflowSilently () {
8991 Gson gson = new Gson ();
9092 String json = "{\" value\" : \" 300\" }" ;
9193 ByteExample model = gson .fromJson (json , ByteExample .class );
9294
9395 assertEquals (44 , model .value );
9496 }
9597
96- @ Test public void fromJsonRealToByte () {
98+ @ Test public void whenDeserializingRealIntoByte_thenShouldRaiseAnException () {
9799 Gson gson = new Gson ();
98100 String json = "{\" value\" : 2.3}" ;
99101 try {
@@ -107,7 +109,7 @@ public class UnitTest {
107109 fail ();
108110 }
109111
110- @ Test public void fromJsonRealToLong () {
112+ @ Test public void whenDeserializingRealIntoLong_thenShouldRaiseAnException () {
111113 Gson gson = new Gson ();
112114 String json = "{\" value\" : 2.3}" ;
113115 try {
@@ -121,22 +123,22 @@ public class UnitTest {
121123 fail ();
122124 }
123125
124- @ Test public void fromJsonRealToLongEndingIn0 () {
126+ @ Test public void whenDeserializingRealWhoseDecimalPartIs0_thenShouldParseItCorrectly () {
125127 Gson gson = new Gson ();
126128 String json = "{\" value\" : 2.0}" ;
127129 LongExample model = gson .fromJson (json , LongExample .class );
128130 assertEquals (2 , model .value );
129131 }
130132
131- @ Test public void fromJsonUnicodeChar () {
133+ @ Test public void whenDeserializingUnicodeChar_thenShouldParseItCorrectly () {
132134 Gson gson = new Gson ();
133135 String json = "{\" value\" : \" \\ u00AE\" }" ;
134136 CharExample model = gson .fromJson (json , CharExample .class );
135137
136138 assertEquals ('\u00AE' , model .value );
137139 }
138140
139- @ Test public void fromJsonNull () {
141+ @ Test public void whenDeserializingNullValues_thenShouldIgnoreThoseFields () {
140142 Gson gson = new Gson ();
141143 // @formatter:off
142144 String json = "{\" byteValue\" : null, \" shortValue\" : null, "
@@ -154,7 +156,8 @@ public class UnitTest {
154156 assertEquals (1 , model .doubleValue , 0.0001 );
155157 }
156158
157- @ Test (expected = JsonSyntaxException .class ) public void fromJsonEmptyString () {
159+ @ Test (expected = JsonSyntaxException .class ) public void
160+ whenDeserializingTheEmptyString_thenShouldRaiseAnException () {
158161 Gson gson = new Gson ();
159162 // @formatter:off
160163 String json = "{\" byteValue\" : \" \" , \" shortValue\" : \" \" , "
@@ -164,7 +167,7 @@ public class UnitTest {
164167 gson .fromJson (json , PrimitiveBundleInitialized .class );
165168 }
166169
167- @ Test public void fromJsonEmptyStringToChar () {
170+ @ Test public void whenDeserializingTheEmptyStringIntoChar_thenShouldHaveTheEmtpyChar () {
168171 Gson gson = new Gson ();
169172 // @formatter:off
170173 String json = "{\" charValue\" : \" \" }" ;
@@ -174,7 +177,7 @@ public class UnitTest {
174177 assertEquals (Character .MIN_VALUE , model .value );
175178 }
176179
177- @ Test public void fromJsonValidValueWithinString () {
180+ @ Test public void whenDeserializingValidValueAppearingInAString_thenShouldParseTheValue () {
178181 Gson gson = new Gson ();
179182 // @formatter:off
180183 String json = "{\" byteValue\" : \" 15\" , \" shortValue\" : \" 15\" , "
@@ -192,7 +195,7 @@ public class UnitTest {
192195 assertEquals (15 , model .doubleValue , 0.0001 );
193196 }
194197
195- @ Test public void fromJsonBooleanFrom2ValueInteger () {
198+ @ Test public void whenDeserializingABooleanFrom0Or1Integer_thenShouldRaiseAnException () {
196199 String json = "{\" value\" : 1}" ;
197200 Gson gson = new Gson ();
198201
@@ -207,7 +210,7 @@ public class UnitTest {
207210 fail ();
208211 }
209212
210- @ Test public void fromJsonBooleanFrom2ValueIntegerSolution () {
213+ @ Test public void whenDeserializingWithCustomDeserializerABooleanFrom0Or1Integer_thenShouldWork () {
211214 String json = "{\" value\" : 1}" ;
212215 GsonBuilder builder = new GsonBuilder ();
213216 builder .registerTypeAdapter (BooleanExample .class ,
@@ -220,25 +223,6 @@ public class UnitTest {
220223 assertTrue (model .value );
221224 }
222225
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-
242226 // @formatter:off
243227 static class BooleanAs2ValueIntegerDeserializer implements JsonDeserializer <BooleanExample > {
244228 @ Override public BooleanExample deserialize (
0 commit comments