Back to Repositories

Testing Field Exclusion Mechanisms in google/gson

This test suite validates field exclusion functionality in Google’s Gson library, focusing on serialization and deserialization of inner classes and nested static classes. The tests ensure proper handling of field inclusion/exclusion based on Gson configuration settings.

Test Coverage Overview

The test suite provides comprehensive coverage of Gson’s field exclusion mechanisms.

Key areas tested include:
  • Default inner class serialization behavior
  • Explicit inner class exclusion
  • Nested static class handling
  • Custom serialization configuration
Edge cases cover both default and custom Gson builder configurations, ensuring consistent behavior across different initialization approaches.

Implementation Analysis

The testing approach utilizes JUnit’s test framework with a systematic verification of Gson’s serialization outcomes.

Implementation patterns include:
  • Setup fixtures using @Before annotation
  • Comparison of serialized JSON against expected string formats
  • Testing both default and custom Gson configurations
  • Verification using Truth assertion library

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Google Truth assertion library
  • Gson and GsonBuilder for JSON processing
  • Custom nested class hierarchy for testing
  • Structured test fixtures with @Before setup

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming reflecting test purpose
  • Proper test isolation and setup
  • Consistent assertion patterns
  • Comprehensive coverage of feature variations
  • Effective use of helper methods and class hierarchy

google/gson

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

            
/*
 * Copyright (C) 2008 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.GsonBuilder;
import org.junit.Before;
import org.junit.Test;

/**
 * Performs some functional testing to ensure GSON infrastructure properly serializes/deserializes
 * fields that either should or should not be included in the output based on the GSON
 * configuration.
 *
 * @author Joel Leitch
 */
public class FieldExclusionTest {
  private static final String VALUE = "blah_1234";

  private Outer outer;

  @Before
  public void setUp() throws Exception {
    outer = new Outer();
  }

  @Test
  public void testDefaultInnerClassExclusion() {
    Gson gson = new Gson();
    Outer.Inner target = outer.new Inner(VALUE);
    String result = gson.toJson(target);
    assertThat(result).isEqualTo(target.toJson());

    gson = new GsonBuilder().create();
    target = outer.new Inner(VALUE);
    result = gson.toJson(target);
    assertThat(result).isEqualTo(target.toJson());
  }

  @Test
  public void testInnerClassExclusion() {
    Gson gson = new GsonBuilder().disableInnerClassSerialization().create();
    Outer.Inner target = outer.new Inner(VALUE);
    String result = gson.toJson(target);
    assertThat(result).isEqualTo("null");
  }

  @Test
  public void testDefaultNestedStaticClassIncluded() {
    Gson gson = new Gson();
    Outer.Inner target = outer.new Inner(VALUE);
    String result = gson.toJson(target);
    assertThat(result).isEqualTo(target.toJson());

    gson = new GsonBuilder().create();
    target = outer.new Inner(VALUE);
    result = gson.toJson(target);
    assertThat(result).isEqualTo(target.toJson());
  }

  private static class Outer {

    @SuppressWarnings("ClassCanBeStatic")
    private class Inner extends NestedClass {
      public Inner(String value) {
        super(value);
      }
    }
  }

  private static class NestedClass {
    private final String value;

    public NestedClass(String value) {
      this.value = value;
    }

    public String toJson() {
      return "{\"value\":\"" + value + "\"}";
    }
  }
}