Skip to content

Commit 1a2170b

Browse files
authored
Port tests from JUnit 3 to JUnit 4 (#2294)
* Port tests from JUnit 3 to JUnit 4 * Port tests from JUnit 3 to JUnit 4 * Add `@Test` above `@Ignore`
1 parent 4aaf138 commit 1a2170b

File tree

97 files changed

+1903
-543
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1903
-543
lines changed

extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.google.gson.interceptors;
1717

18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.fail;
20+
1821
import com.google.gson.Gson;
1922
import com.google.gson.GsonBuilder;
2023
import com.google.gson.JsonParseException;
@@ -29,50 +32,55 @@
2932
import java.util.List;
3033
import java.util.Map;
3134
import java.util.Map.Entry;
32-
import junit.framework.TestCase;
35+
import org.junit.Before;
36+
import org.junit.Test;
3337

3438
/**
3539
* Unit tests for {@link Intercept} and {@link JsonPostDeserializer}.
3640
*
3741
* @author Inderjeet Singh
3842
*/
39-
public final class InterceptorTest extends TestCase {
43+
public final class InterceptorTest {
4044

4145
private Gson gson;
4246

43-
@Override
47+
@Before
4448
public void setUp() throws Exception {
45-
super.setUp();
4649
this.gson = new GsonBuilder()
4750
.registerTypeAdapterFactory(new InterceptorFactory())
4851
.enableComplexMapKeySerialization()
4952
.create();
5053
}
5154

55+
@Test
5256
public void testExceptionsPropagated() {
5357
try {
5458
gson.fromJson("{}", User.class);
5559
fail();
5660
} catch (JsonParseException expected) {}
5761
}
5862

63+
@Test
5964
public void testTopLevelClass() {
6065
User user = gson.fromJson("{name:'bob',password:'pwd'}", User.class);
6166
assertEquals(User.DEFAULT_EMAIL, user.email);
6267
}
6368

69+
@Test
6470
public void testList() {
6571
List<User> list = gson.fromJson("[{name:'bob',password:'pwd'}]", new TypeToken<List<User>>(){}.getType());
6672
User user = list.get(0);
6773
assertEquals(User.DEFAULT_EMAIL, user.email);
6874
}
6975

76+
@Test
7077
public void testCollection() {
7178
Collection<User> list = gson.fromJson("[{name:'bob',password:'pwd'}]", new TypeToken<Collection<User>>(){}.getType());
7279
User user = list.iterator().next();
7380
assertEquals(User.DEFAULT_EMAIL, user.email);
7481
}
7582

83+
@Test
7684
public void testMapKeyAndValues() {
7785
Type mapType = new TypeToken<Map<User, Address>>(){}.getType();
7886
try {
@@ -86,11 +94,13 @@ public void testMapKeyAndValues() {
8694
assertEquals(Address.DEFAULT_FIRST_LINE, entry.getValue().firstLine);
8795
}
8896

97+
@Test
8998
public void testField() {
9099
UserGroup userGroup = gson.fromJson("{user:{name:'bob',password:'pwd'}}", UserGroup.class);
91100
assertEquals(User.DEFAULT_EMAIL, userGroup.user.email);
92101
}
93102

103+
@Test
94104
public void testCustomTypeAdapter() {
95105
Gson gson = new GsonBuilder()
96106
.registerTypeAdapter(User.class, new TypeAdapter<User>() {
@@ -114,6 +124,7 @@ public void testCustomTypeAdapter() {
114124
assertEquals(User.DEFAULT_EMAIL, userGroup.user.email);
115125
}
116126

127+
@Test
117128
public void testDirectInvocationOfTypeAdapter() throws Exception {
118129
TypeAdapter<UserGroup> adapter = gson.getAdapter(UserGroup.class);
119130
UserGroup userGroup = adapter.fromJson("{\"user\":{\"name\":\"bob\",\"password\":\"pwd\"}}");

extras/src/test/java/com/google/gson/typeadapters/PostConstructAdapterFactoryTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616

1717
package com.google.gson.typeadapters;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.fail;
21+
1922
import com.google.gson.Gson;
2023
import com.google.gson.GsonBuilder;
2124
import java.util.Arrays;
2225
import java.util.List;
2326
import javax.annotation.PostConstruct;
24-
import junit.framework.TestCase;
27+
import org.junit.Test;
2528

26-
public class PostConstructAdapterFactoryTest extends TestCase {
27-
public void test() throws Exception {
29+
public class PostConstructAdapterFactoryTest {
30+
@Test
31+
public void test() throws Exception {
2832
Gson gson = new GsonBuilder()
2933
.registerTypeAdapterFactory(new PostConstructAdapterFactory())
3034
.create();
@@ -37,7 +41,8 @@ public void test() throws Exception {
3741
}
3842
}
3943

40-
public void testList() {
44+
@Test
45+
public void testList() {
4146
MultipleSandwiches sandwiches = new MultipleSandwiches(Arrays.asList(
4247
new Sandwich("white", "cheddar"),
4348
new Sandwich("whole wheat", "swiss")));

extras/src/test/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactoryTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@
1616

1717
package com.google.gson.typeadapters;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.fail;
23+
1924
import com.google.gson.Gson;
2025
import com.google.gson.GsonBuilder;
2126
import com.google.gson.JsonParseException;
2227
import com.google.gson.TypeAdapterFactory;
23-
import junit.framework.TestCase;
28+
import org.junit.Test;
2429

25-
public final class RuntimeTypeAdapterFactoryTest extends TestCase {
30+
public final class RuntimeTypeAdapterFactoryTest {
2631

32+
@Test
2733
public void testRuntimeTypeAdapter() {
2834
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
2935
BillingInstrument.class)
@@ -41,6 +47,7 @@ public void testRuntimeTypeAdapter() {
4147
assertTrue(deserialized instanceof CreditCard);
4248
}
4349

50+
@Test
4451
public void testRuntimeTypeAdapterRecognizeSubtypes() {
4552
// We don't have an explicit factory for CreditCard.class, but we do have one for
4653
// BillingInstrument.class that has recognizeSubtypes(). So it should recognize CreditCard, and
@@ -62,6 +69,7 @@ public void testRuntimeTypeAdapterRecognizeSubtypes() {
6269
assertTrue(deserialized instanceof CreditCard);
6370
}
6471

72+
@Test
6573
public void testRuntimeTypeIsBaseType() {
6674
TypeAdapterFactory rta = RuntimeTypeAdapterFactory.of(
6775
BillingInstrument.class)
@@ -78,6 +86,7 @@ public void testRuntimeTypeIsBaseType() {
7886
assertEquals("Jesse", deserialized.ownerName);
7987
}
8088

89+
@Test
8190
public void testNullBaseType() {
8291
try {
8392
RuntimeTypeAdapterFactory.of(null);
@@ -86,6 +95,7 @@ public void testNullBaseType() {
8695
}
8796
}
8897

98+
@Test
8999
public void testNullTypeFieldName() {
90100
try {
91101
RuntimeTypeAdapterFactory.of(BillingInstrument.class, null);
@@ -94,6 +104,7 @@ public void testNullTypeFieldName() {
94104
}
95105
}
96106

107+
@Test
97108
public void testNullSubtype() {
98109
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
99110
BillingInstrument.class);
@@ -104,6 +115,7 @@ public void testNullSubtype() {
104115
}
105116
}
106117

118+
@Test
107119
public void testNullLabel() {
108120
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
109121
BillingInstrument.class);
@@ -114,6 +126,7 @@ public void testNullLabel() {
114126
}
115127
}
116128

129+
@Test
117130
public void testDuplicateSubtype() {
118131
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
119132
BillingInstrument.class);
@@ -125,6 +138,7 @@ public void testDuplicateSubtype() {
125138
}
126139
}
127140

141+
@Test
128142
public void testDuplicateLabel() {
129143
RuntimeTypeAdapterFactory<BillingInstrument> rta = RuntimeTypeAdapterFactory.of(
130144
BillingInstrument.class);
@@ -136,6 +150,7 @@ public void testDuplicateLabel() {
136150
}
137151
}
138152

153+
@Test
139154
public void testDeserializeMissingTypeField() {
140155
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
141156
.registerSubtype(CreditCard.class);
@@ -149,6 +164,7 @@ public void testDeserializeMissingTypeField() {
149164
}
150165
}
151166

167+
@Test
152168
public void testDeserializeMissingSubtype() {
153169
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
154170
.registerSubtype(BankTransfer.class);
@@ -162,6 +178,7 @@ public void testDeserializeMissingSubtype() {
162178
}
163179
}
164180

181+
@Test
165182
public void testSerializeMissingSubtype() {
166183
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
167184
.registerSubtype(BankTransfer.class);
@@ -175,6 +192,7 @@ public void testSerializeMissingSubtype() {
175192
}
176193
}
177194

195+
@Test
178196
public void testSerializeCollidingTypeFieldName() {
179197
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class, "cvv")
180198
.registerSubtype(CreditCard.class);
@@ -188,6 +206,7 @@ public void testSerializeCollidingTypeFieldName() {
188206
}
189207
}
190208

209+
@Test
191210
public void testSerializeWrappedNullValue() {
192211
TypeAdapterFactory billingAdapter = RuntimeTypeAdapterFactory.of(BillingInstrument.class)
193212
.registerSubtype(CreditCard.class)

extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.google.gson.typeadapters;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.fail;
21+
1922
import com.google.gson.Gson;
2023
import com.google.gson.GsonBuilder;
2124
import com.google.gson.JsonParseException;
@@ -24,20 +27,22 @@
2427
import java.util.Date;
2528
import java.util.Locale;
2629
import java.util.TimeZone;
27-
import junit.framework.TestCase;
30+
import org.junit.Test;
2831

29-
public final class UtcDateTypeAdapterTest extends TestCase {
32+
public final class UtcDateTypeAdapterTest {
3033
private final Gson gson = new GsonBuilder()
3134
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
3235
.create();
3336

37+
@Test
3438
public void testLocalTimeZone() {
3539
Date expected = new Date();
3640
String json = gson.toJson(expected);
3741
Date actual = gson.fromJson(json, Date.class);
3842
assertEquals(expected.getTime(), actual.getTime());
3943
}
4044

45+
@Test
4146
public void testDifferentTimeZones() {
4247
for (String timeZone : TimeZone.getAvailableIDs()) {
4348
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
@@ -53,13 +58,15 @@ public void testDifferentTimeZones() {
5358
* JDK 1.7 introduced support for XXX format to indicate UTC date. But Android is older JDK.
5459
* We want to make sure that this date is parseable in Android.
5560
*/
61+
@Test
5662
public void testUtcDatesOnJdkBefore1_7() {
5763
Gson gson = new GsonBuilder()
5864
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
5965
.create();
6066
gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class);
6167
}
6268

69+
@Test
6370
public void testUtcWithJdk7Default() {
6471
Date expected = new Date();
6572
SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US);
@@ -71,11 +78,13 @@ public void testUtcWithJdk7Default() {
7178
assertEquals(expected.getTime(), actual.getTime());
7279
}
7380

81+
@Test
7482
public void testNullDateSerialization() {
7583
String json = gson.toJson(null, Date.class);
7684
assertEquals("null", json);
7785
}
7886

87+
@Test
7988
public void testWellFormedParseException() {
8089
try {
8190
gson.fromJson("2017-06-20T14:32:30", Date.class);

gson/src/test/java/com/google/gson/CommentsTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@
1616

1717
package com.google.gson;
1818

19+
import static org.junit.Assert.assertEquals;
20+
1921
import com.google.gson.reflect.TypeToken;
2022
import java.util.Arrays;
2123
import java.util.List;
22-
import junit.framework.TestCase;
24+
import org.junit.Test;
2325

2426
/**
2527
* @author Jesse Wilson
2628
*/
27-
public final class CommentsTest extends TestCase {
29+
public final class CommentsTest {
2830

2931
/**
3032
* Test for issue 212.
3133
*/
34+
@Test
3235
public void testParseComments() {
3336
String json = "[\n"
3437
+ " // this is a comment\n"

gson/src/test/java/com/google/gson/DefaultInetAddressTypeAdapterTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,26 @@
1616

1717
package com.google.gson;
1818

19-
import java.net.InetAddress;
19+
import static org.junit.Assert.assertEquals;
2020

21-
import junit.framework.TestCase;
21+
import java.net.InetAddress;
22+
import org.junit.Before;
23+
import org.junit.Test;
2224

2325
/**
2426
* Unit tests for the default serializer/deserializer for the {@code InetAddress} type.
2527
*
2628
* @author Joel Leitch
2729
*/
28-
public class DefaultInetAddressTypeAdapterTest extends TestCase {
30+
public class DefaultInetAddressTypeAdapterTest {
2931
private Gson gson;
3032

31-
@Override
32-
protected void setUp() throws Exception {
33-
super.setUp();
33+
@Before
34+
public void setUp() throws Exception {
3435
gson = new Gson();
3536
}
3637

38+
@Test
3739
public void testInetAddressSerializationAndDeserialization() throws Exception {
3840
InetAddress address = InetAddress.getByName("8.8.8.8");
3941
String jsonAddress = gson.toJson(address);

0 commit comments

Comments
 (0)