Back to Repositories

Testing Glide Initialization Edge Cases in bumptech/glide

This test suite validates the initialization behavior and edge cases of the Glide image loading library. It focuses on testing exception handling during initialization and verifying the initialization state management.

Test Coverage Overview

The test suite provides comprehensive coverage of Glide’s initialization process, focusing on edge cases and error conditions.

Key areas tested include:
  • Exception propagation during initialization
  • Multiple initialization attempts with exceptions
  • Initialization state verification
  • Synchronization handling during initialization

Implementation Analysis

The testing approach employs JUnit4 with AndroidJUnit4 runner for Android-specific testing context. The implementation uses synchronized blocks to ensure thread-safe initialization testing and leverages custom exception handling to verify error scenarios.

Notable patterns include:
  • Use of TearDownGlide rule for test isolation
  • Anonymous inner classes for dynamic test scenarios
  • Assertion combinations using Truth and JUnit assertions

Technical Details

Testing infrastructure includes:
  • JUnit4 testing framework
  • AndroidJUnit4 test runner
  • Google Truth assertion library
  • Custom TearDownGlide rule
  • ApplicationProvider for context management
  • Synchronized block handling for thread safety

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Android development.

Notable practices include:
  • Proper test isolation using @Rule
  • Comprehensive edge case coverage
  • Clear test method naming conventions
  • Effective use of setup and teardown mechanisms
  • Robust exception testing patterns

bumptech/glide

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

            
package com.bumptech.glide;

import static com.google.common.truth.Truth.assertThat;
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.tests.TearDownGlide;
import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import org.junit.runner.RunWith;

// This test is about edge cases that might otherwise make debugging more challenging.
@RunWith(AndroidJUnit4.class)
public class InitializeGlideTest {
  @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 = 2515021766931124927L;
  }

  @Test
  public void initialize_whenInternalMethodThrows_throwsException() {
    assertThrows(
        TestException.class,
        new ThrowingRunnable() {
          @Override
          public void run() {
            synchronized (Glide.class) {
              Glide.checkAndInitializeGlide(
                  context,
                  new GeneratedAppGlideModule() {
                    @NonNull
                    @Override
                    Set<Class<?>> getExcludedModuleClasses() {
                      throw new TestException();
                    }
                  });
            }
          }
        });
  }

  @Test
  public void initialize_whenInternalMethodThrows_andCalledTwice_throwsException() {
    GeneratedAppGlideModule throwingGeneratedAppGlideModule =
        new GeneratedAppGlideModule() {
          @NonNull
          @Override
          Set<Class<?>> getExcludedModuleClasses() {
            throw new TestException();
          }
        };
    ThrowingRunnable initializeGlide =
        new ThrowingRunnable() {
          @Override
          public void run() throws Throwable {
            synchronized (Glide.class) {
              Glide.checkAndInitializeGlide(context, throwingGeneratedAppGlideModule);
            }
          }
        };

    assertThrows(TestException.class, initializeGlide);
    // Make sure the second exception isn't hidden by some Glide initialization related exception.
    assertThrows(TestException.class, initializeGlide);
  }

  @Test
  public void isInitialized_whenNotInitialized_returnsFalse() {
    assertThat(Glide.isInitialized()).isFalse();
  }

  @Test
  public void isInitialized_whenInitialized_returnsTrue() {
    Glide.get(context);

    assertThat(Glide.isInitialized()).isTrue();
  }
}