Back to Repositories

Testing Raw Type Serialization Workflows in Google Gson

This test suite validates Gson’s ability to serialize parameterized types without explicit type information. It examines raw serialization behavior for collections and nested generic objects, ensuring proper JSON conversion across different complexity levels.

Test Coverage Overview

The test suite provides comprehensive coverage of raw serialization scenarios in Gson.

Key areas tested include:
  • Collection serialization for primitives and objects
  • Single-level parameterized object serialization
  • Multi-level nested generic object serialization (up to 3 levels)
  • Comparison of explicit vs implicit type handling

Implementation Analysis

The testing approach uses JUnit to validate Gson’s serialization capabilities systematically. Each test case compares the generated JSON output against expected string representations, utilizing both explicit TypeToken specifications and implicit type inference.

Technical patterns include:
  • Setup of Gson instance using @Before
  • Comparison testing using Truth assertions
  • Generic type handling with TypeToken

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Google Truth assertion library
  • Gson library for JSON serialization
  • Custom test classes (Foo and Bar) for generic type testing
  • TypeToken for explicit type information

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable approaches include:
  • Systematic complexity progression from simple to complex cases
  • Clear test method naming conventions
  • Proper test setup isolation
  • Comprehensive edge case coverage
  • Consistent assertion patterns

google/gson

gson/src/test/java/com/google/gson/functional/RawSerializationTest.java

            
/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.gson.functional;

import static com.google.common.truth.Truth.assertThat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests to validate serialization of parameterized types without explicit types
 *
 * @author Inderjeet Singh
 */
public class RawSerializationTest {

  private Gson gson;

  @Before
  public void setUp() throws Exception {
    gson = new Gson();
  }

  @Test
  public void testCollectionOfPrimitives() {
    Collection<Integer> ints = Arrays.asList(1, 2, 3, 4, 5);
    String json = gson.toJson(ints);
    assertThat(json).isEqualTo("[1,2,3,4,5]");
  }

  @Test
  public void testCollectionOfObjects() {
    Collection<Foo> foos = Arrays.asList(new Foo(1), new Foo(2));
    String json = gson.toJson(foos);
    assertThat(json).isEqualTo("[{\"b\":1},{\"b\":2}]");
  }

  @Test
  public void testParameterizedObject() {
    Bar<Foo> bar = new Bar<>(new Foo(1));
    String expectedJson = "{\"t\":{\"b\":1}}";
    // Ensure that serialization works without specifying the type explicitly
    String json = gson.toJson(bar);
    assertThat(json).isEqualTo(expectedJson);
    // Ensure that serialization also works when the type is specified explicitly
    json = gson.toJson(bar, new TypeToken<Bar<Foo>>() {}.getType());
    assertThat(json).isEqualTo(expectedJson);
  }

  @Test
  public void testTwoLevelParameterizedObject() {
    Bar<Bar<Foo>> bar = new Bar<>(new Bar<>(new Foo(1)));
    String expectedJson = "{\"t\":{\"t\":{\"b\":1}}}";
    // Ensure that serialization works without specifying the type explicitly
    String json = gson.toJson(bar);
    assertThat(json).isEqualTo(expectedJson);
    // Ensure that serialization also works when the type is specified explicitly
    json = gson.toJson(bar, new TypeToken<Bar<Bar<Foo>>>() {}.getType());
    assertThat(json).isEqualTo(expectedJson);
  }

  @Test
  public void testThreeLevelParameterizedObject() {
    Bar<Bar<Bar<Foo>>> bar = new Bar<>(new Bar<>(new Bar<>(new Foo(1))));
    String expectedJson = "{\"t\":{\"t\":{\"t\":{\"b\":1}}}}";
    // Ensure that serialization works without specifying the type explicitly
    String json = gson.toJson(bar);
    assertThat(json).isEqualTo(expectedJson);
    // Ensure that serialization also works when the type is specified explicitly
    json = gson.toJson(bar, new TypeToken<Bar<Bar<Bar<Foo>>>>() {}.getType());
    assertThat(json).isEqualTo(expectedJson);
  }

  private static class Foo {
    @SuppressWarnings("unused")
    int b;

    Foo(int b) {
      this.b = b;
    }
  }

  private static class Bar<T> {
    @SuppressWarnings("unused")
    T t;

    Bar(T t) {
      this.t = t;
    }
  }
}