Skip to content

Fix HTTP Mock Services expectation setting for Testing#2706

Merged
Umang01-hash merged 5 commits intodevelopmentfrom
fix-http-mock
Dec 12, 2025
Merged

Fix HTTP Mock Services expectation setting for Testing#2706
Umang01-hash merged 5 commits intodevelopmentfrom
fix-http-mock

Conversation

@coolwednesday
Copy link
Member

@coolwednesday coolwednesday commented Dec 12, 2025

Support for Multiple Independent HTTP Service Mocks in Testing

Summary

  • Closes Consistent Tests in examples and documentation #2704
  • This PR introduces support for testing handlers that interact with multiple HTTP services by providing independent mock instances for each service. The previous single mock instance approach (mocks.HTTPService) has been deprecated in favor of a map-based approach (mocks.HTTPServices) that allows each service to have its own isolated mock expectations.

What Was Deprecated

mocks.HTTPService (Singular Field)

Previous Usage:

mockContainer, mocks := container.NewMockContainer(t, 
    container.WithMockHTTPService("serviceName"),
)

// All services would share the same mock instance
mocks.HTTPService.EXPECT().Get(...).Return(...)

Why It Was Deprecated:

  1. Single Mock Instance Limitation: The old approach provided only one mock instance (mocks.HTTPService) that was shared across all services. This made it impossible to:

    • Set different expectations for different services
    • Test handlers that call multiple HTTP services independently
    • Isolate test scenarios for individual services
  2. Confusion in Multi-Service Scenarios: When testing handlers that interact with multiple services (e.g., payment service, shipping service, notification service), developers couldn't distinguish which service was being called, leading to:

    • Ambiguous test failures
    • Difficulty in setting service-specific expectations
    • Inability to test different response scenarios per service
  3. Backward Compatibility: The field is kept for backward compatibility but marked as deprecated and will be removed in a future version.

What Has Been Added

1. mocks.HTTPServices (Map-Based Approach)

New Usage:

// Register multiple services - each gets its own separate mock instance
mockContainer, mocks := container.NewMockContainer(t,
    container.WithMockHTTPService("paymentService", "shippingService", "notificationService"),
)

// Each service has independent expectations
mocks.HTTPServices["paymentService"].EXPECT().Get(...).Return(paymentResp, nil)
mocks.HTTPServices["shippingService"].EXPECT().Get(...).Return(shippingResp, nil)
mocks.HTTPServices["notificationService"].EXPECT().Get(...).Return(notificationResp, nil)

Key Benefits:

  1. Independent Mock Instances: Each service registered via WithMockHTTPService() gets its own separate mock instance, allowing:

    • Different expectations for each service
    • Service-specific error scenarios
    • Independent test isolation
  2. Service Name Mapping: Services are accessed by name using mocks.HTTPServices["serviceName"], making it clear which service is being configured.

  3. Multiple Service Support: Handlers that call multiple HTTP services can now be properly tested with each service having its own mock expectations.

2. Enhanced WithMockHTTPService() Function

The function now:

  • Accepts multiple service names: WithMockHTTPService("service1", "service2", ...)
  • Creates a separate mock instance for each service name
  • Returns a map of service names to their mock instances
  • Maintains backward compatibility with the deprecated HTTPService field

3. Comprehensive Testing Documentation

  • Updated Example Tests (gofr/examples/http-server/main_test.go):
    • Demonstrates correct usage of mocks.HTTPServices["serviceName"]
    • Shows independent mock service expectations
    • Includes comprehensive test cases for multiple services
    • Updated TestTraceHandler to use the new pattern

Migration Guide

For Single Service (Backward Compatible)

Old Way (Still Works):

mocks.HTTPService.EXPECT().Get(...).Return(...)

New Way (Recommended):

mocks.HTTPServices["serviceName"].EXPECT().Get(...).Return(...)

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

@Umang01-hash Umang01-hash merged commit d41f125 into development Dec 12, 2025
20 checks passed
@Umang01-hash Umang01-hash deleted the fix-http-mock branch December 12, 2025 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consistent Tests in examples and documentation

2 participants