Back to Repositories

Testing Constructor Instantiation Restrictions in Google Gson

This test suite validates the behavior of the ConstructorConstructor class in Google’s Gson library, focusing on handling abstract classes and interfaces during object construction. It ensures proper error handling and messaging when attempting to instantiate non-instantiable types.

Test Coverage Overview

The test suite provides comprehensive coverage of ConstructorConstructor’s error handling mechanisms.

Key areas tested include:
  • Abstract class instantiation attempts
  • Interface instantiation attempts
  • Error message validation for both scenarios
  • Runtime exception handling

Implementation Analysis

The testing approach utilizes JUnit’s assertion framework combined with Google Truth for enhanced assertion readability. The tests employ a systematic pattern of attempting invalid constructions and verifying the resulting exception messages, ensuring proper guidance is provided to developers.

Technical implementation features:
  • Use of TypeToken for type information handling
  • Empty constructor configuration
  • Exception message validation

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Google Truth assertion library
  • TypeToken for generic type handling
  • Mock abstract classes and interfaces
  • Runtime exception verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming conventions
  • Proper test isolation
  • Comprehensive error message validation
  • Documentation of test purposes
  • Efficient test setup with minimal dependencies

google/gson

gson/src/test/java/com/google/gson/internal/ConstructorConstructorTest.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.internal;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.gson.reflect.TypeToken;
import java.util.Collections;
import org.junit.Test;

public class ConstructorConstructorTest {
  private ConstructorConstructor constructorConstructor =
      new ConstructorConstructor(Collections.emptyMap(), true, Collections.emptyList());

  private abstract static class AbstractClass {
    @SuppressWarnings("unused")
    public AbstractClass() {}
  }

  private interface Interface {}

  /**
   * Verify that ConstructorConstructor does not try to invoke no-args constructor of abstract
   * class.
   */
  @Test
  public void testGet_AbstractClassNoArgConstructor() {
    ObjectConstructor<AbstractClass> constructor =
        constructorConstructor.get(TypeToken.get(AbstractClass.class));
    var e = assertThrows(RuntimeException.class, () -> constructor.construct());
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            "Abstract classes can't be instantiated! Adjust the R8 configuration or register an"
                + " InstanceCreator or a TypeAdapter for this type. Class name:"
                + " com.google.gson.internal.ConstructorConstructorTest$AbstractClass\n"
                + "See https://github.com/google/gson/blob/main/Troubleshooting.md#r8-abstract-class");
  }

  @Test
  public void testGet_Interface() {
    ObjectConstructor<Interface> constructor =
        constructorConstructor.get(TypeToken.get(Interface.class));
    var e = assertThrows(RuntimeException.class, () -> constructor.construct());
    assertThat(e)
        .hasMessageThat()
        .isEqualTo(
            "Interfaces can't be instantiated! Register an InstanceCreator or a TypeAdapter for"
                + " this type. Interface name:"
                + " com.google.gson.internal.ConstructorConstructorTest$Interface");
  }
}