Back to Repositories

Testing Object TypeAdapter JSON Serialization in google/gson

This test suite validates the Object TypeAdapter functionality in Google’s GSON library, focusing on parameterized testing of JSON serialization and deserialization. It ensures consistent handling of various JSON data structures and primitive types through round-trip conversion testing.

Test Coverage Overview

The test suite provides comprehensive coverage of GSON’s Object TypeAdapter, testing various JSON input formats including arrays, objects, primitives, and nested structures. Key functionality tested includes:

  • Null value handling
  • Primitive type conversion (numbers, booleans, strings)
  • Complex nested JSON structures
  • Empty arrays and objects
  • Mixed-type JSON arrays and objects

Implementation Analysis

The implementation uses JUnit’s parameterized testing framework to efficiently test multiple JSON input scenarios. The testing approach leverages the @RunWith(Parameterized.class) annotation to execute the same test logic across different JSON inputs, ensuring thorough validation of the TypeAdapter’s behavior.

The test pattern follows a round-trip verification approach, first deserializing JSON to Objects then re-serializing to verify exact string matches.

Technical Details

Testing tools and configuration:

  • JUnit 4 with Parameterized test runner
  • Google Truth assertion library
  • GSON TypeAdapter for Object class
  • Test parameters defined via @Parameters annotation
  • IOException handling for JSON processing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Parameterized testing to reduce code duplication
  • Clear test method naming (testReadWrite)
  • Comprehensive input test cases
  • Strong assertions using Google Truth
  • Efficient test organization with single responsibility principle

google/gson

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

            
/*
 * Copyright (C) 2022 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;

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

import java.io.IOException;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ObjectTypeAdapterParameterizedTest {
  @Parameters
  public static Iterable<String> data() {
    return Arrays.asList(
        "[]",
        "{}",
        "null",
        "1.0",
        "true",
        "\"string\"",
        "[true,1.0,null,{},2.0,{\"a\":[false]},[3.0,\"test\"],4.0]",
        "{\"\":1.0,\"a\":true,\"b\":null,\"c\":[],\"d\":{\"a1\":2.0,\"b2\":[true,{\"a3\":3.0}]},\"e\":[{\"f\":4.0},\"test\"]}");
  }

  private final TypeAdapter<Object> adapter = new Gson().getAdapter(Object.class);
  @Parameter public String json;

  @Test
  public void testReadWrite() throws IOException {
    Object deserialized = adapter.fromJson(json);
    String actualSerialized = adapter.toJson(deserialized);

    // Serialized Object should be the same as original JSON
    assertThat(actualSerialized).isEqualTo(json);
  }
}