diff --git a/gson/pom.xml b/gson/pom.xml
index 58cb47e6..bfaad784 100644
--- a/gson/pom.xml
+++ b/gson/pom.xml
@@ -27,6 +27,12 @@
junit
test
+
+ com.google.truth
+ truth
+ 1.1.3
+ test
+
diff --git a/gson/src/test/java/com/google/gson/CommentsTest.java b/gson/src/test/java/com/google/gson/CommentsTest.java
index 777cbea6..f1bcd948 100644
--- a/gson/src/test/java/com/google/gson/CommentsTest.java
+++ b/gson/src/test/java/com/google/gson/CommentsTest.java
@@ -16,10 +16,9 @@
package com.google.gson;
-import static org.junit.Assert.assertEquals;
+import static com.google.common.truth.Truth.assertThat;
import com.google.gson.reflect.TypeToken;
-import java.util.Arrays;
import java.util.List;
import org.junit.Test;
@@ -43,6 +42,6 @@ public final class CommentsTest {
+ "]";
List abc = new Gson().fromJson(json, new TypeToken>() {}.getType());
- assertEquals(Arrays.asList("a", "b", "c"), abc);
+ assertThat(abc).containsExactly("a", "b", "c").inOrder();
}
}
diff --git a/gson/src/test/java/com/google/gson/DefaultInetAddressTypeAdapterTest.java b/gson/src/test/java/com/google/gson/DefaultInetAddressTypeAdapterTest.java
index ed8220f6..3edee56a 100644
--- a/gson/src/test/java/com/google/gson/DefaultInetAddressTypeAdapterTest.java
+++ b/gson/src/test/java/com/google/gson/DefaultInetAddressTypeAdapterTest.java
@@ -16,7 +16,7 @@
package com.google.gson;
-import static org.junit.Assert.assertEquals;
+import static com.google.common.truth.Truth.assertThat;
import java.net.InetAddress;
import org.junit.Before;
@@ -39,9 +39,9 @@ public class DefaultInetAddressTypeAdapterTest {
public void testInetAddressSerializationAndDeserialization() throws Exception {
InetAddress address = InetAddress.getByName("8.8.8.8");
String jsonAddress = gson.toJson(address);
- assertEquals("\"8.8.8.8\"", jsonAddress);
-
+ assertThat(jsonAddress).isEqualTo("\"8.8.8.8\"");
+
InetAddress value = gson.fromJson(jsonAddress, InetAddress.class);
- assertEquals(value, address);
- }
+ assertThat(address).isEqualTo(value);
+ }
}
diff --git a/gson/src/test/java/com/google/gson/DefaultMapJsonSerializerTest.java b/gson/src/test/java/com/google/gson/DefaultMapJsonSerializerTest.java
index cb0739fe..75bc43e3 100644
--- a/gson/src/test/java/com/google/gson/DefaultMapJsonSerializerTest.java
+++ b/gson/src/test/java/com/google/gson/DefaultMapJsonSerializerTest.java
@@ -16,7 +16,7 @@
package com.google.gson;
-import static org.junit.Assert.assertTrue;
+import static com.google.common.truth.Truth.assertThat;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
@@ -37,9 +37,9 @@ public class DefaultMapJsonSerializerTest {
public void testEmptyMapNoTypeSerialization() {
Map emptyMap = new HashMap<>();
JsonElement element = gson.toJsonTree(emptyMap, emptyMap.getClass());
- assertTrue(element instanceof JsonObject);
+ assertThat(element).isInstanceOf(JsonObject.class);
JsonObject emptyMapJsonObject = (JsonObject) element;
- assertTrue(emptyMapJsonObject.entrySet().isEmpty());
+ assertThat(emptyMapJsonObject.entrySet()).isEmpty();
}
@Test
@@ -48,9 +48,9 @@ public class DefaultMapJsonSerializerTest {
Map emptyMap = new HashMap<>();
JsonElement element = gson.toJsonTree(emptyMap, mapType);
- assertTrue(element instanceof JsonObject);
+ assertThat(element).isInstanceOf(JsonObject.class);
JsonObject emptyMapJsonObject = (JsonObject) element;
- assertTrue(emptyMapJsonObject.entrySet().isEmpty());
+ assertThat(emptyMapJsonObject.entrySet()).isEmpty();
}
@Test
@@ -62,8 +62,8 @@ public class DefaultMapJsonSerializerTest {
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(myMap, mapType);
- assertTrue(element.isJsonObject());
+ assertThat(element.isJsonObject()).isTrue();
JsonObject mapJsonObject = element.getAsJsonObject();
- assertTrue(mapJsonObject.has(key));
+ assertThat(mapJsonObject.has(key)).isTrue();
}
}
diff --git a/gson/src/test/java/com/google/gson/ExposeAnnotationExclusionStrategyTest.java b/gson/src/test/java/com/google/gson/ExposeAnnotationExclusionStrategyTest.java
index fccda63a..17aca036 100644
--- a/gson/src/test/java/com/google/gson/ExposeAnnotationExclusionStrategyTest.java
+++ b/gson/src/test/java/com/google/gson/ExposeAnnotationExclusionStrategyTest.java
@@ -16,8 +16,7 @@
package com.google.gson;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static com.google.common.truth.Truth.assertThat;
import com.google.gson.annotations.Expose;
import com.google.gson.internal.Excluder;
@@ -33,44 +32,44 @@ public class ExposeAnnotationExclusionStrategyTest {
private Excluder excluder = Excluder.DEFAULT.excludeFieldsWithoutExposeAnnotation();
@Test
- public void testNeverSkipClasses() throws Exception {
- assertFalse(excluder.excludeClass(MockObject.class, true));
- assertFalse(excluder.excludeClass(MockObject.class, false));
+ public void testNeverSkipClasses() {
+ assertThat(excluder.excludeClass(MockObject.class, true)).isFalse();
+ assertThat(excluder.excludeClass(MockObject.class, false)).isFalse();
}
@Test
public void testSkipNonAnnotatedFields() throws Exception {
Field f = createFieldAttributes("hiddenField");
- assertTrue(excluder.excludeField(f, true));
- assertTrue(excluder.excludeField(f, false));
+ assertThat(excluder.excludeField(f, true)).isTrue();
+ assertThat(excluder.excludeField(f, false)).isTrue();
}
@Test
public void testSkipExplicitlySkippedFields() throws Exception {
Field f = createFieldAttributes("explicitlyHiddenField");
- assertTrue(excluder.excludeField(f, true));
- assertTrue(excluder.excludeField(f, false));
+ assertThat(excluder.excludeField(f, true)).isTrue();
+ assertThat(excluder.excludeField(f, false)).isTrue();
}
@Test
public void testNeverSkipExposedAnnotatedFields() throws Exception {
Field f = createFieldAttributes("exposedField");
- assertFalse(excluder.excludeField(f, true));
- assertFalse(excluder.excludeField(f, false));
+ assertThat(excluder.excludeField(f, true)).isFalse();
+ assertThat(excluder.excludeField(f, false)).isFalse();
}
@Test
public void testNeverSkipExplicitlyExposedAnnotatedFields() throws Exception {
Field f = createFieldAttributes("explicitlyExposedField");
- assertFalse(excluder.excludeField(f, true));
- assertFalse(excluder.excludeField(f, false));
+ assertThat(excluder.excludeField(f, true)).isFalse();
+ assertThat(excluder.excludeField(f, false)).isFalse();
}
@Test
public void testDifferentSerializeAndDeserializeField() throws Exception {
Field f = createFieldAttributes("explicitlyDifferentModeField");
- assertFalse(excluder.excludeField(f, true));
- assertTrue(excluder.excludeField(f, false));
+ assertThat(excluder.excludeField(f, true)).isFalse();
+ assertThat(excluder.excludeField(f, false)).isTrue();
}
private static Field createFieldAttributes(String fieldName) throws Exception {
diff --git a/gson/src/test/java/com/google/gson/FieldAttributesTest.java b/gson/src/test/java/com/google/gson/FieldAttributesTest.java
index 2cc362d2..5e79c1f1 100644
--- a/gson/src/test/java/com/google/gson/FieldAttributesTest.java
+++ b/gson/src/test/java/com/google/gson/FieldAttributesTest.java
@@ -16,9 +16,7 @@
package com.google.gson;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import com.google.gson.reflect.TypeToken;
@@ -44,7 +42,7 @@ public class FieldAttributesTest {
@SuppressWarnings("unused")
@Test
- public void testNullField() throws Exception {
+ public void testNullField() {
try {
new FieldAttributes(null);
fail("Field parameter can not be null");
@@ -52,32 +50,32 @@ public class FieldAttributesTest {
}
@Test
- public void testDeclaringClass() throws Exception {
- assertEquals(Foo.class, fieldAttributes.getDeclaringClass());
+ public void testDeclaringClass() {
+ assertThat(fieldAttributes.getDeclaringClass()).isAssignableTo(Foo.class);
}
@Test
- public void testModifiers() throws Exception {
- assertFalse(fieldAttributes.hasModifier(Modifier.STATIC));
- assertFalse(fieldAttributes.hasModifier(Modifier.FINAL));
- assertFalse(fieldAttributes.hasModifier(Modifier.ABSTRACT));
- assertFalse(fieldAttributes.hasModifier(Modifier.VOLATILE));
- assertFalse(fieldAttributes.hasModifier(Modifier.PROTECTED));
+ public void testModifiers() {
+ assertThat(fieldAttributes.hasModifier(Modifier.STATIC)).isFalse();
+ assertThat(fieldAttributes.hasModifier(Modifier.FINAL)).isFalse();
+ assertThat(fieldAttributes.hasModifier(Modifier.ABSTRACT)).isFalse();
+ assertThat(fieldAttributes.hasModifier(Modifier.VOLATILE)).isFalse();
+ assertThat(fieldAttributes.hasModifier(Modifier.PROTECTED)).isFalse();
- assertTrue(fieldAttributes.hasModifier(Modifier.PUBLIC));
- assertTrue(fieldAttributes.hasModifier(Modifier.TRANSIENT));
+ assertThat(fieldAttributes.hasModifier(Modifier.PUBLIC)).isTrue();
+ assertThat(fieldAttributes.hasModifier(Modifier.TRANSIENT)).isTrue();
}
@Test
- public void testName() throws Exception {
- assertEquals("bar", fieldAttributes.getName());
+ public void testName() {
+ assertThat(fieldAttributes.getName()).isEqualTo("bar");
}
@Test
- public void testDeclaredTypeAndClass() throws Exception {
+ public void testDeclaredTypeAndClass() {
Type expectedType = new TypeToken>() {}.getType();
- assertEquals(expectedType, fieldAttributes.getDeclaredType());
- assertEquals(List.class, fieldAttributes.getDeclaredClass());
+ assertThat(fieldAttributes.getDeclaredType()).isEqualTo(expectedType);
+ assertThat(fieldAttributes.getDeclaredClass()).isAssignableTo(List.class);
}
private static class Foo {
diff --git a/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java b/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java
index 4d4c716b..ebb7f0aa 100644
--- a/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java
+++ b/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java
@@ -1,7 +1,8 @@
package com.google.gson;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.common.truth.Truth.assertWithMessage;
+
import java.lang.reflect.Field;
import java.util.Locale;
import org.junit.Test;
@@ -28,7 +29,7 @@ public class FieldNamingPolicyTest {
};
for (String[] pair : argumentPairs) {
- assertEquals(pair[1], FieldNamingPolicy.separateCamelCase(pair[0], '_'));
+ assertThat(FieldNamingPolicy.separateCamelCase(pair[0], '_')).isEqualTo(pair[1]);
}
}
@@ -51,12 +52,12 @@ public class FieldNamingPolicyTest {
};
for (String[] pair : argumentPairs) {
- assertEquals(pair[1], FieldNamingPolicy.upperCaseFirstLetter(pair[0]));
+ assertThat(FieldNamingPolicy.upperCaseFirstLetter(pair[0])).isEqualTo(pair[1]);
}
}
/**
- * Upper casing policies should be unaffected by default Locale.
+ * Upper-casing policies should be unaffected by default Locale.
*/
@Test
public void testUpperCasingLocaleIndependent() throws Exception {
@@ -81,11 +82,13 @@ public class FieldNamingPolicyTest {
try {
// Verify that default Locale has different case conversion rules
- assertNotEquals("Test setup is broken", expected, name.toUpperCase());
+ assertWithMessage("Test setup is broken")
+ .that(name.toUpperCase()).doesNotMatch(expected);
for (FieldNamingPolicy policy : policies) {
// Should ignore default Locale
- assertEquals("Unexpected conversion for " + policy, expected, policy.translateName(field));
+ assertWithMessage("Unexpected conversion for %s", policy)
+ .that(policy.translateName(field)).matches(expected);
}
} finally {
Locale.setDefault(oldLocale);
@@ -118,11 +121,13 @@ public class FieldNamingPolicyTest {
try {
// Verify that default Locale has different case conversion rules
- assertNotEquals("Test setup is broken", expected, name.toLowerCase());
+ assertWithMessage("Test setup is broken")
+ .that(name.toLowerCase()).doesNotMatch(expected);
for (FieldNamingPolicy policy : policies) {
// Should ignore default Locale
- assertEquals("Unexpected conversion for " + policy, expected, policy.translateName(field));
+ assertWithMessage("Unexpected conversion for %s", policy)
+ .that(policy.translateName(field)).matches(expected);
}
} finally {
Locale.setDefault(oldLocale);
diff --git a/gson/src/test/java/com/google/gson/GenericArrayTypeTest.java b/gson/src/test/java/com/google/gson/GenericArrayTypeTest.java
index 58185486..4b300d25 100644
--- a/gson/src/test/java/com/google/gson/GenericArrayTypeTest.java
+++ b/gson/src/test/java/com/google/gson/GenericArrayTypeTest.java
@@ -16,8 +16,7 @@
package com.google.gson;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
+import static com.google.common.truth.Truth.assertThat;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.reflect.TypeToken;
@@ -46,15 +45,15 @@ public class GenericArrayTypeTest {
Type parameterizedType = new TypeToken>() {}.getType();
Type genericArrayType = new TypeToken[]>() {}.getType();
- assertEquals(parameterizedType, ourType.getGenericComponentType());
- assertEquals(genericArrayType, ourType);
- assertEquals(genericArrayType.hashCode(), ourType.hashCode());
+ assertThat(ourType.getGenericComponentType()).isEqualTo(parameterizedType);
+ assertThat(ourType).isEqualTo(genericArrayType);
+ assertThat(ourType.hashCode()).isEqualTo(genericArrayType.hashCode());
}
@Test
public void testNotEquals() throws Exception {
Type differentGenericArrayType = new TypeToken[][]>() {}.getType();
- assertFalse(differentGenericArrayType.equals(ourType));
- assertFalse(ourType.equals(differentGenericArrayType));
+ assertThat(differentGenericArrayType.equals(ourType)).isFalse();
+ assertThat(ourType.equals(differentGenericArrayType)).isFalse();
}
}
diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java
index e1a013b5..4c6d5ec9 100644
--- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java
+++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java
@@ -16,9 +16,7 @@
package com.google.gson;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNotSame;
+import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import com.google.gson.stream.JsonReader;
@@ -48,8 +46,8 @@ public class GsonBuilderTest {
public void testCreatingMoreThanOnce() {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
- assertNotNull(gson);
- assertNotNull(builder.create());
+ assertThat(gson).isNotNull();
+ assertThat(builder.create()).isNotNull();
builder.setFieldNamingStrategy(new FieldNamingStrategy() {
@Override public String translateName(Field f) {
@@ -58,9 +56,9 @@ public class GsonBuilderTest {
});
Gson otherGson = builder.create();
- assertNotNull(otherGson);
+ assertThat(otherGson).isNotNull();
// Should be different instances because builder has been modified in the meantime
- assertNotSame(gson, otherGson);
+ assertThat(gson).isNotSameInstanceAs(otherGson);
}
/**
@@ -74,7 +72,7 @@ public class GsonBuilderTest {
// Modifications of `gsonBuilder` should not affect `gson` object
gsonBuilder.registerTypeAdapter(CustomClass1.class, new TypeAdapter() {
- @Override public CustomClass1 read(JsonReader in) throws IOException {
+ @Override public CustomClass1 read(JsonReader in) {
throw new UnsupportedOperationException();
}
@@ -93,6 +91,7 @@ public class GsonBuilderTest {
}
});
+
assertDefaultGson(gson);
// New GsonBuilder created from `gson` should not have been affected by changes
// to `gsonBuilder` either
@@ -105,26 +104,26 @@ public class GsonBuilderTest {
private static void assertDefaultGson(Gson gson) {
// Should use default reflective adapter
String json1 = gson.toJson(new CustomClass1());
- assertEquals("{}", json1);
+ assertThat(json1).isEqualTo("{}");
// Should use default reflective adapter
String json2 = gson.toJson(new CustomClass2());
- assertEquals("{}", json2);
+ assertThat(json2).isEqualTo("{}");
// Should use default instance creator
CustomClass3 customClass3 = gson.fromJson("{}", CustomClass3.class);
- assertEquals(CustomClass3.NO_ARG_CONSTRUCTOR_VALUE, customClass3.s);
+ assertThat(customClass3.s).isEqualTo(CustomClass3.NO_ARG_CONSTRUCTOR_VALUE);
}
private static void assertCustomGson(Gson gson) {
String json1 = gson.toJson(new CustomClass1());
- assertEquals("\"custom-adapter\"", json1);
+ assertThat(json1).isEqualTo("\"custom-adapter\"");
String json2 = gson.toJson(new CustomClass2());
- assertEquals("\"custom-hierarchy-adapter\"", json2);
+ assertThat(json2).isEqualTo("\"custom-hierarchy-adapter\"");
CustomClass3 customClass3 = gson.fromJson("{}", CustomClass3.class);
- assertEquals("custom-instance", customClass3.s);
+ assertThat(customClass3.s).isEqualTo("custom-instance");
}
static class CustomClass1 { }
@@ -148,7 +147,7 @@ public class GsonBuilderTest {
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.VOLATILE, Modifier.PRIVATE)
.create();
- assertEquals("{\"d\":\"d\"}", gson.toJson(new HasModifiers()));
+ assertThat(gson.toJson(new HasModifiers())).isEqualTo("{\"d\":\"d\"}");
}
@SuppressWarnings("unused")
@@ -164,7 +163,7 @@ public class GsonBuilderTest {
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers()
.create();
- assertEquals("{\"a\":\"a\"}", gson.toJson(new HasTransients()));
+ assertThat(gson.toJson(new HasTransients())).isEqualTo("{\"a\":\"a\"}");
}
static class HasTransients {
@@ -195,12 +194,10 @@ public class GsonBuilderTest {
gson.fromJson("{}", ClassWithoutNoArgsConstructor.class);
fail("Expected exception");
} catch (JsonIOException expected) {
- assertEquals(
- "Unable to create instance of class com.google.gson.GsonBuilderTest$ClassWithoutNoArgsConstructor; "
- + "usage of JDK Unsafe is disabled. Registering an InstanceCreator or a TypeAdapter for this type, "
- + "adding a no-args constructor, or enabling usage of JDK Unsafe may fix this problem.",
- expected.getMessage()
- );
+ assertThat(expected).hasMessageThat().isEqualTo(
+ "Unable to create instance of class com.google.gson.GsonBuilderTest$ClassWithoutNoArgsConstructor; "
+ + "usage of JDK Unsafe is disabled. Registering an InstanceCreator or a TypeAdapter for this type, "
+ + "adding a no-args constructor, or enabling usage of JDK Unsafe may fix this problem.");
}
}
@@ -217,14 +214,14 @@ public class GsonBuilderTest {
builder.setVersion(Double.NaN);
fail();
} catch (IllegalArgumentException e) {
- assertEquals("Invalid version: NaN", e.getMessage());
+ assertThat(e).hasMessageThat().isEqualTo("Invalid version: NaN");
}
try {
builder.setVersion(-0.1);
fail();
} catch (IllegalArgumentException e) {
- assertEquals("Invalid version: -0.1", e.getMessage());
+ assertThat(e).hasMessageThat().isEqualTo("Invalid version: -0.1");
}
}
}
diff --git a/gson/src/test/java/com/google/gson/GsonTest.java b/gson/src/test/java/com/google/gson/GsonTest.java
index fd335e49..706d06ad 100644
--- a/gson/src/test/java/com/google/gson/GsonTest.java
+++ b/gson/src/test/java/com/google/gson/GsonTest.java
@@ -16,8 +16,7 @@
package com.google.gson;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import com.google.gson.Gson.FutureTypeAdapter;
@@ -70,10 +69,10 @@ public final class GsonTest {
CUSTOM_OBJECT_TO_NUMBER_STRATEGY, CUSTOM_NUMBER_TO_NUMBER_STRATEGY,
Collections.emptyList());
- assertEquals(CUSTOM_EXCLUDER, gson.excluder);
- assertEquals(CUSTOM_FIELD_NAMING_STRATEGY, gson.fieldNamingStrategy());
- assertEquals(true, gson.serializeNulls());
- assertEquals(false, gson.htmlSafe());
+ assertThat(gson.excluder).isEqualTo(CUSTOM_EXCLUDER);
+ assertThat(gson.fieldNamingStrategy()).isEqualTo(CUSTOM_FIELD_NAMING_STRATEGY);
+ assertThat(gson.serializeNulls()).isTrue();
+ assertThat(gson.htmlSafe()).isFalse();
}
@Test
@@ -90,14 +89,14 @@ public final class GsonTest {
.registerTypeAdapter(Object.class, new TestTypeAdapter())
.create();
- assertEquals(original.factories.size() + 1, clone.factories.size());
+ assertThat(clone.factories).hasSize(original.factories.size() + 1);
}
private static final class TestTypeAdapter extends TypeAdapter