Back to Repositories

Testing Registry Creation Exception Handling in Glide

This test suite validates the RegistryFactory functionality in Glide, focusing on error handling and registry initialization behavior. It specifically tests the handling of exceptions during component registration and ensures consistent behavior across multiple registry creation attempts.

Test Coverage Overview

The test suite covers critical error handling scenarios in Glide’s RegistryFactory.

Key areas tested include:
  • Exception propagation during registry creation
  • Multiple invocation behavior with failing modules
  • Consistency of error handling across repeated calls
  • Integration with AppGlideModule registration flow

Implementation Analysis

The testing approach employs JUnit4 with AndroidJUnit4 runner for Android-specific testing context. It utilizes anonymous inner classes to create test scenarios and implements custom exception handling verification.

Notable patterns include:
  • Use of TearDownGlide rule for test isolation
  • Implementation of custom TestException for specific error tracking
  • Utilization of assertThrows for exception validation

Technical Details

Testing tools and configuration:
  • JUnit4 testing framework
  • AndroidJUnit4 test runner
  • ApplicationProvider for context management
  • Custom TearDownGlide rule
  • ImmutableList for module management
  • GlideSupplier for lazy registry creation

Best Practices Demonstrated

The test implementation showcases several testing best practices in Android development.

Notable practices include:
  • Proper test isolation using @Rule
  • Clear test method naming convention
  • Effective use of anonymous classes for test scenarios
  • Robust exception handling verification
  • Proper cleanup mechanisms

bumptech/glide

library/test/src/test/java/com/bumptech/glide/RegistryFactoryTest.java

            
package com.bumptech.glide;

import static org.junit.Assert.assertThrows;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.tests.TearDownGlide;
import com.bumptech.glide.util.GlideSuppliers.GlideSupplier;
import com.google.common.collect.ImmutableList;
import org.junit.Rule;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class RegistryFactoryTest {
  @Rule public final TearDownGlide tearDownGlide = new TearDownGlide();
  private final Context context = ApplicationProvider.getApplicationContext();

  private static final class TestException extends RuntimeException {
    private static final long serialVersionUID = 2334956185897161236L;
  }

  @Test
  public void create_whenCalledTwiceWithThrowingModule_throwsOriginalException() {
    AppGlideModule throwingAppGlideModule =
        new AppGlideModule() {
          @Override
          public void registerComponents(
              @NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
            throw new TestException();
          }
        };

    Glide glide = Glide.get(context);
    GlideSupplier<Registry> registrySupplier =
        RegistryFactory.lazilyCreateAndInitializeRegistry(
            glide, /* manifestModules= */ ImmutableList.of(), throwingAppGlideModule);

    assertThrows(
        TestException.class,
        new ThrowingRunnable() {
          @Override
          public void run() {
            registrySupplier.get();
          }
        });

    assertThrows(
        TestException.class,
        new ThrowingRunnable() {
          @Override
          public void run() {
            registrySupplier.get();
          }
        });
  }
}