diff --git a/extras/pom.xml b/extras/pom.xml
index 020ab45f48..5fb9df63ac 100644
--- a/extras/pom.xml
+++ b/extras/pom.xml
@@ -65,6 +65,11 @@
junit
test
+
+ com.google.truth
+ truth
+ test
+
diff --git a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java
index fa70755abd..f705cd4628 100644
--- a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java
+++ b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java
@@ -173,8 +173,8 @@ private RuntimeTypeAdapterFactory(Class> baseType, String typeFieldName, boole
}
/**
- * Creates a new runtime type adapter using for {@code baseType} using {@code typeFieldName} as
- * the type field name. Type field names are case sensitive.
+ * Creates a new runtime type adapter for {@code baseType} using {@code typeFieldName} as the type
+ * field name. Type field names are case sensitive.
*
* @param maintainType true if the type field should be included in deserialized objects
*/
@@ -184,8 +184,8 @@ public static RuntimeTypeAdapterFactory of(
}
/**
- * Creates a new runtime type adapter using for {@code baseType} using {@code typeFieldName} as
- * the type field name. Type field names are case sensitive.
+ * Creates a new runtime type adapter for {@code baseType} using {@code typeFieldName} as the type
+ * field name. Type field names are case sensitive.
*/
public static RuntimeTypeAdapterFactory of(Class baseType, String typeFieldName) {
return new RuntimeTypeAdapterFactory<>(baseType, typeFieldName, false);
diff --git a/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java b/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java
index 5f4b986f53..9c79130d0d 100644
--- a/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java
+++ b/extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java
@@ -16,15 +16,13 @@
package com.google.gson.graph;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
+import static com.google.common.truth.Truth.assertThat;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import org.junit.Test;
@@ -42,11 +40,11 @@ public void testSerialization() {
new GraphAdapterBuilder().addType(Roshambo.class).registerOn(gsonBuilder);
Gson gson = gsonBuilder.create();
- assertEquals(
- "{'0x1':{'name':'ROCK','beats':'0x2'},"
- + "'0x2':{'name':'SCISSORS','beats':'0x3'},"
- + "'0x3':{'name':'PAPER','beats':'0x1'}}",
- gson.toJson(rock).replace('"', '\''));
+ assertThat(gson.toJson(rock).replace('"', '\''))
+ .isEqualTo(
+ "{'0x1':{'name':'ROCK','beats':'0x2'},"
+ + "'0x2':{'name':'SCISSORS','beats':'0x3'},"
+ + "'0x3':{'name':'PAPER','beats':'0x1'}}");
}
@Test
@@ -61,12 +59,12 @@ public void testDeserialization() {
Gson gson = gsonBuilder.create();
Roshambo rock = gson.fromJson(json, Roshambo.class);
- assertEquals("ROCK", rock.name);
+ assertThat(rock.name).isEqualTo("ROCK");
Roshambo scissors = rock.beats;
- assertEquals("SCISSORS", scissors.name);
+ assertThat(scissors.name).isEqualTo("SCISSORS");
Roshambo paper = scissors.beats;
- assertEquals("PAPER", paper.name);
- assertSame(rock, paper.beats);
+ assertThat(paper.name).isEqualTo("PAPER");
+ assertThat(paper.beats).isSameInstanceAs(rock);
}
@Test
@@ -78,8 +76,8 @@ public void testDeserializationDirectSelfReference() {
Gson gson = gsonBuilder.create();
Roshambo suicide = gson.fromJson(json, Roshambo.class);
- assertEquals("SUICIDE", suicide.name);
- assertSame(suicide, suicide.beats);
+ assertThat(suicide.name).isEqualTo("SUICIDE");
+ assertThat(suicide.beats).isSameInstanceAs(suicide);
}
@Test
@@ -99,7 +97,7 @@ public void testSerializeListOfLists() {
Gson gson = gsonBuilder.create();
String json = gson.toJson(listOfLists, listOfListsType);
- assertEquals("{'0x1':['0x1','0x2'],'0x2':[]}", json.replace('"', '\''));
+ assertThat(json.replace('"', '\'')).isEqualTo("{'0x1':['0x1','0x2'],'0x2':[]}");
}
@Test
@@ -115,9 +113,9 @@ public void testDeserializeListOfLists() {
Gson gson = gsonBuilder.create();
List> listOfLists = gson.fromJson("{'0x1':['0x1','0x2'],'0x2':[]}", listOfListsType);
- assertEquals(2, listOfLists.size());
- assertSame(listOfLists, listOfLists.get(0));
- assertEquals(Collections.emptyList(), listOfLists.get(1));
+ assertThat(listOfLists).hasSize(2);
+ assertThat(listOfLists.get(0)).isSameInstanceAs(listOfLists);
+ assertThat(listOfLists.get(1)).isEmpty();
}
@Test
@@ -134,11 +132,11 @@ public void testSerializationWithMultipleTypes() {
.registerOn(gsonBuilder);
Gson gson = gsonBuilder.create();
- assertEquals(
- "{'0x1':{'name':'Google','employees':['0x2','0x3']},"
- + "'0x2':{'name':'Jesse','company':'0x1'},"
- + "'0x3':{'name':'Joel','company':'0x1'}}",
- gson.toJson(google).replace('"', '\''));
+ assertThat(gson.toJson(google).replace('"', '\''))
+ .isEqualTo(
+ "{'0x1':{'name':'Google','employees':['0x2','0x3']},"
+ + "'0x2':{'name':'Jesse','company':'0x1'},"
+ + "'0x3':{'name':'Joel','company':'0x1'}}");
}
@Test
@@ -155,13 +153,13 @@ public void testDeserializationWithMultipleTypes() {
+ "'0x2':{'name':'Jesse','company':'0x1'},"
+ "'0x3':{'name':'Joel','company':'0x1'}}";
Company company = gson.fromJson(json, Company.class);
- assertEquals("Google", company.name);
+ assertThat(company.name).isEqualTo("Google");
Employee jesse = company.employees.get(0);
- assertEquals("Jesse", jesse.name);
- assertEquals(company, jesse.company);
+ assertThat(jesse.name).isEqualTo("Jesse");
+ assertThat(jesse.company).isEqualTo(company);
Employee joel = company.employees.get(1);
- assertEquals("Joel", joel.name);
- assertEquals(company, joel.company);
+ assertThat(joel.name).isEqualTo("Joel");
+ assertThat(joel.company).isEqualTo(company);
}
static class Roshambo {
diff --git a/extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java b/extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java
index 9a957fc744..6c5cc91fa6 100644
--- a/extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java
+++ b/extras/src/test/java/com/google/gson/interceptors/InterceptorTest.java
@@ -15,8 +15,8 @@
*/
package com.google.gson.interceptors;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertThrows;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -55,17 +55,14 @@ public void setUp() throws Exception {
@Test
public void testExceptionsPropagated() {
- try {
- gson.fromJson("{}", User.class);
- fail();
- } catch (JsonParseException expected) {
- }
+ var e = assertThrows(JsonParseException.class, () -> gson.fromJson("{}", User.class));
+ assertThat(e).hasMessageThat().isEqualTo("name and password are required fields.");
}
@Test
public void testTopLevelClass() {
User user = gson.fromJson("{name:'bob',password:'pwd'}", User.class);
- assertEquals(User.DEFAULT_EMAIL, user.email);
+ assertThat(user.email).isEqualTo(User.DEFAULT_EMAIL);
}
@Test
@@ -73,7 +70,7 @@ public void testList() {
List list =
gson.fromJson("[{name:'bob',password:'pwd'}]", new TypeToken>() {}.getType());
User user = list.get(0);
- assertEquals(User.DEFAULT_EMAIL, user.email);
+ assertThat(user.email).isEqualTo(User.DEFAULT_EMAIL);
}
@Test
@@ -82,30 +79,31 @@ public void testCollection() {
gson.fromJson(
"[{name:'bob',password:'pwd'}]", new TypeToken>() {}.getType());
User user = list.iterator().next();
- assertEquals(User.DEFAULT_EMAIL, user.email);
+ assertThat(user.email).isEqualTo(User.DEFAULT_EMAIL);
}
@Test
public void testMapKeyAndValues() {
Type mapType = new TypeToken