Back to Repositories

Testing SimpleResource Object Management in Glide Library

This test suite validates the SimpleResource class functionality in the Glide library, focusing on resource object handling and null value validation. The tests ensure proper object retrieval and error handling behaviors.

Test Coverage Overview

The test suite provides comprehensive coverage of SimpleResource class operations.

Key areas tested include:
  • Basic object retrieval functionality
  • Multiple retrieval consistency
  • Null input validation
The tests verify both positive scenarios and error conditions, ensuring robust resource handling.

Implementation Analysis

The testing approach utilizes JUnit4 framework features for systematic validation of the SimpleResource class.

Implementation highlights:
  • Test setup using @Before annotation for resource initialization
  • Multiple assertion patterns for object equality verification
  • Exception testing using expected parameter

Technical Details

Testing infrastructure includes:
  • JUnit4 test runner and annotations
  • Assert methods for equality verification
  • Custom Anything class for test object creation
  • SimpleResource wrapper class testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming conventions
  • Proper test setup isolation
  • Comprehensive edge case coverage
  • Effective exception handling validation
  • Clean and focused test methods

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/resource/SimpleResourceTest.java

            
package com.bumptech.glide.load.resource;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SimpleResourceTest {
  private Anything object;
  private SimpleResource<?> resource;

  @Before
  public void setUp() {
    object = new Anything();
    resource = new SimpleResource<>(object);
  }

  @Test
  public void testReturnsGivenObject() {
    assertEquals(object, resource.get());
  }

  @Test
  public void testReturnsGivenObjectMultipleTimes() {
    assertEquals(object, resource.get());
    assertEquals(object, resource.get());
    assertEquals(object, resource.get());
  }

  @Test(expected = NullPointerException.class)
  public void testThrowsIfGivenNullData() {
    new SimpleResource<>(null);
  }

  private static class Anything {}
}