Back to Repositories

Testing SerializedName Annotation Behavior in Google GSON

This test suite validates the functionality of SerializedName annotations in Google’s GSON library, focusing on serialization and deserialization behavior with multiple alternate names for JSON fields.

Test Coverage Overview

The test suite provides comprehensive coverage of SerializedName annotation functionality in GSON:

  • Verification of primary name serialization
  • Testing deserialization with multiple alternate names
  • Handling of multiple serialized names in the same JSON string
  • Edge case validation for name precedence

Implementation Analysis

The testing approach utilizes JUnit framework with Google Truth assertions for precise verification. The tests implement a systematic pattern of checking both serialization and deserialization scenarios, using a custom MyClass test fixture with annotated fields.

Key patterns include:
  • Direct JSON string comparison for serialization tests
  • Multiple deserialization scenarios with varied input
  • Precedence verification for conflicting names

Technical Details

Testing infrastructure includes:

  • JUnit 4 test framework
  • Google Truth assertion library
  • GSON instance for JSON operations
  • Custom test class with @SerializedName annotations
  • Java class-level encapsulation for test isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming reflecting test purpose
  • Isolated test cases with specific assertions
  • Comprehensive coverage of both positive and edge cases
  • Proper test fixture setup with minimal complexity
  • Effective use of assertion libraries for readable verification

google/gson

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

            
/*
 * Copyright (C) 2015 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.annotations.SerializedName;
import org.junit.Test;

public final class SerializedNameTest {
  private final Gson gson = new Gson();

  @Test
  public void testFirstNameIsChosenForSerialization() {
    MyClass target = new MyClass("v1", "v2");
    // Ensure name1 occurs exactly once, and name2 and name3 don't appear
    assertThat(gson.toJson(target)).isEqualTo("{\"name\":\"v1\",\"name1\":\"v2\"}");
  }

  @Test
  public void testMultipleNamesDeserializedCorrectly() {
    assertThat(gson.fromJson("{'name':'v1'}", MyClass.class).a).isEqualTo("v1");

    // Both name1 and name2 gets deserialized to b
    assertThat(gson.fromJson("{'name1':'v11'}", MyClass.class).b).isEqualTo("v11");
    assertThat(gson.fromJson("{'name2':'v2'}", MyClass.class).b).isEqualTo("v2");
    assertThat(gson.fromJson("{'name3':'v3'}", MyClass.class).b).isEqualTo("v3");
  }

  @Test
  public void testMultipleNamesInTheSameString() {
    // The last value takes precedence
    assertThat(gson.fromJson("{'name1':'v1','name2':'v2','name3':'v3'}", MyClass.class).b)
        .isEqualTo("v3");
  }

  private static final class MyClass {
    @SerializedName("name")
    String a;

    @SerializedName(
        value = "name1",
        alternate = {"name2", "name3"})
    String b;

    MyClass(String a, String b) {
      this.a = a;
      this.b = b;
    }
  }
}