Skip to content

Class Name: GeolocationServiceMock

Last Updated: 2025-10-22 Source Code: GeolocationServiceMock.cls

API Name: GeolocationServiceMock Type: Test Utility (HttpCalloutMock) Test Coverage: N/A (Test utility class)

Business Purpose

This test utility class provides a mock HTTP response for geolocation service integrations, enabling reliable unit testing of address validation and geocoding functionality without making actual external API calls. It supports AANP's testing infrastructure for location-based features and address validation processes.

Class Overview

Scope and Sharing

  • Sharing Model: N/A (Test class - runs in system mode)
  • Access Modifier: global
  • Interfaces Implemented: HttpCalloutMock

Key Responsibilities

  • Provides mock HTTP responses for geolocation service testing
  • Returns predefined address/geolocation data from static resources
  • Eliminates external API dependencies during test execution
  • Ensures consistent and reliable test data for location-based functionality
  • Simulates successful HTTP 200 responses with JSON payloads

Public Methods

respond

public HttpResponse respond(HttpRequest request)

Purpose: Generates a mock HTTP response for geolocation service callouts during test execution.

Parameters: - request (HttpRequest): The HTTP request being mocked (not used in current implementation)

Returns: - HttpResponse: Mock response containing address data from static resource

Throws: - QueryException: If 'AddressResponse' static resource is not found

Usage Example:

@IsTest
public class MyGeolocationTest {
    @IsTest
    static void testAddressValidation() {
        Test.setMock(HttpCalloutMock.class, new GeolocationServiceMock());

        Test.startTest();
        // Call method that makes geolocation callout
        MyGeolocationService.validateAddress('123 Main St');
        Test.stopTest();

        // Assert results
    }
}

Business Logic: - Queries StaticResource object for 'AddressResponse' resource - Creates HttpResponse with JSON content type - Converts static resource body to string - Returns HTTP 200 status code to simulate success - Does not examine or use the request parameter


Private/Helper Methods

No private helper methods in this class.


Dependencies

Apex Classes

  • None (standalone test utility)

Salesforce Objects

  • StaticResource: Queries for 'AddressResponse' resource

Custom Settings/Metadata

  • None

External Services

  • None (mocks external services for testing)

Static Resources Required: - AddressResponse: Must contain valid JSON response data matching the expected format from actual geolocation service

Design Patterns

  • Mock Object Pattern: Implements HttpCalloutMock to simulate external service responses
  • Test Fixture Pattern: Uses static resource to provide consistent test data
  • Dependency Injection Pattern: Injected into tests via Test.setMock()

Governor Limits Considerations

SOQL Queries: 1 query to retrieve static resource DML Operations: None CPU Time: Minimal - simple string conversion Heap Size: Depends on size of AddressResponse static resource

Bulkification: N/A (test utility) Async Processing: N/A (synchronous test context only)

Note: This class runs only in test context and does not count against production governor limits.

Error Handling

Strategy: No error handling implemented Logging: None User Notifications: N/A (test context only)

Critical Gaps: - No try-catch for SOQL query failure if static resource doesn't exist - No null check on static resource query result - No validation of static resource body content

Security Considerations

Sharing Rules: N/A (test class) Field-Level Security: N/A (static resource query only) CRUD Permissions: N/A (test context) Input Validation: No validation of request parameter

Security Notes: - Annotated with @IsTest - can only be used in test context - Global access modifier allows use across all test classes in the org

Test Class

Test Class: N/A - This is a test utility class Coverage: N/A Used By: - Any test class that requires mocking of geolocation service callouts - Tests for address validation functionality - Tests for practice site location features - Tests for shipping address validation

Changes & History

No change history documented.

Pre-Go-Live Concerns

CRITICAL - Fix Before Go-Live

  • MISSING STATIC RESOURCE: Verify 'AddressResponse' static resource exists and contains valid JSON
  • NO ERROR HANDLING: Missing try-catch for SOQL query failure
  • NO NULL CHECKS: Missing null checks for static resource query results
  • QUERY IN RESPOND METHOD: SOQL query executes on every callout - should cache static resource

HIGH - Address Soon After Go-Live

  • No request parameter validation: Doesn't examine or validate the incoming request
  • No response variation: All requests return the same response regardless of input
  • Missing error scenario testing: Only simulates successful responses (200), no error codes

MEDIUM - Future Enhancement

  • Parameterize response data: Allow different test scenarios with varied response data
  • Support multiple static resources: Different responses based on request parameters
  • Add error condition mocking: Support testing of failed callouts (404, 500, timeout, etc.)
  • Cache static resource: Query once and cache result instead of querying on every respond() call

LOW - Monitor

  • Static resource content validation: Ensure AddressResponse remains valid for testing needs
  • Test effectiveness: Monitor if mock responses adequately simulate production scenarios
  • Request parameter usage: Consider using request URL or body to vary responses

Maintenance Notes

Complexity: Low Recommended Review Schedule: Annually or when geolocation service contract changes

Key Maintainer Notes: - The 'AddressResponse' static resource is critical - class will fail if it doesn't exist - Static resource must contain valid JSON matching the expected response format from actual geolocation service - If geolocation service response format changes, update the static resource accordingly - Consider creating multiple static resources for different test scenarios (valid address, invalid address, service errors) - Global access modifier means any test class can use this mock - Currently performs SOQL query on every mock invocation - consider caching if performance becomes an issue

Critical Dependencies: - AddressResponse static resource must exist and contain valid JSON - Response format must match what consuming code expects from real geolocation service - Any changes to external geolocation API response format require updating the static resource

Testing Strategy Notes: - Use this mock in all tests that make geolocation callouts - Avoid testing actual external API in unit tests - Consider integration tests with real service in a separate test suite