Mockery
Mockery
Automated mock generation for Go interfaces
Mockery is a mock code generation tool used in the Control Plane to generate mocks for interfaces.
Auto-generated test doubles
Mockery automatically generates type-safe mock implementations of Go interfaces, saving development time and ensuring mocks stay in sync with interface changes.
Overview
Mockery automatically generates mock implementations of Go interfaces to use in tests. This helps:
Why Mockery?
🔄 Isolate Components
Test components in isolation without real dependencies for faster, more focused tests.
🎮 Control Behavior
Precisely control how dependencies respond during tests to simulate various scenarios.
✅ Verify Interactions
Confirm that components interact with dependencies correctly with expected arguments.
🔄 Stay in Sync
Auto-generated mocks update when interfaces change, preventing test/implementation drift.
⚡ Test Edge Cases
Easily simulate error conditions and edge cases that would be difficult with real implementations.
🚀 Faster Tests
Avoid slow external dependencies like databases and APIs for quicker test execution.
Integration in the Control Plane
Comprehensive mocking
The Control Plane uses Mockery extensively to enable thorough testing of all components without external dependencies.
Backend Interfaces
Core storage and processing interfaces like:
- FileUploader
- FileDownloader
- FileDeleter
- FileMetadata
Client Interfaces
External service clients such as:
- MinioClient
- K8sClient
- VaultClient
- TokenService
Service Interfaces
Business logic interfaces including:
- FileService
- AuthService
- ValidationService
- NotificationService
Configuration
Mock configuration and generation
The Control Plane has a standardized approach to configuring and generating mocks across the codebase.
Directory Structure
The Control Plane follows a consistent pattern for organizing mock implementations to make them easy to find and use. Mocks are typically placed in a mock
subdirectory of the package that contains the interface being mocked.
Code Generation
Automated generation
Mocks are generated automatically during the build process, ensuring they're always up to date with the latest interface definitions.
Mocks are generated using the Mockery CLI tool. Configuration is stored in mockery.yaml
files.
The tools/generate.go
file ensures consistent mock generation.
Generating Mocks
Manual Generation
To manually generate mocks:
go generate ./tools/generate.go
Or directly using mockery:
mockery --dir pkg/backend --name FileUploader --output pkg/backend/mock
CI/CD Integration
Mocks are automatically verified in CI/CD:
- Pre-commit hooks generate mocks
- CI checks that mocks are up to date
- Tests fail if mocks don't match interfaces
Using Mocks in Tests
Expecter API
The Control Plane uses Mockery's Expecter API, which provides a type-safe way to set expectations on mock methods.
Best Practices Used in Control Plane
Mock best practices
Following these best practices ensures effective and maintainable mocks throughout the codebase.
📝 Generate mocks for all public interfaces
Ensures comprehensive testing coverage and flexibility for all public components.
🎯 Use mock.Anything for irrelevant arguments
Simplifies test setup by only specifying values that matter for the test case.
✅ Verify mock expectations
Always call AssertExpectations() to verify that mocks were used as expected.
🔢 Specify call counts
Use Once(), Times(n), or Maybe() to verify the correct number of interactions.
🧩 Mock at interface boundaries
Focus mocks on external dependencies and service boundaries, not internal implementation details.
🔄 Regenerate mocks after interface changes
Keep mocks in sync with interfaces to avoid subtle test failures or false positives.
Common Mock Patterns
Dependency Injection
The Control Plane uses dependency injection to make components testable.
Test Setup Helpers
Helper functions simplify test setup with mocks.
Related Resources
Testify
Learn about the testing toolkit used alongside Mockery.
Go Language
Explore the foundational language used in the Control Plane.