gomock for Go: Complete Guide to Interface Mocking
\n\n1. What It Is and Why You Need It
\n\nIn modern Go development, testing plays a key role. However, code often depends on external systems: databases, HTTP clients, file systems, or other microservices. Testing such code in isolation is difficult because these dependencies require a real environment. This is where gomock comes to the rescue — a powerful library for generating and using mocks (stubs) for Go interfaces.
\n\ngomock allows you to automatically create interface implementations that behave predictably: return specified values, verify call order, count invocations, and even simulate errors. This makes unit tests fast, deterministic, and independent of external resources. The library integrates tightly with the built-in testing package and is the de facto standard for mocking in the Go community.
The main value of gomock is that you test the behavior of your code, not its implementation. You verify that your service correctly calls dependency methods with the right arguments and handles results properly. This is especially important when following SOLID principles and the Dependency Injection pattern.
\n\n2. Installation
\n\nTo work with gomock, you need to install two components:
\n\n- \n
- gomock library — contains the runtime for working with mocks. \n
- mockgen utility — a code generator for mocks from interfaces. \n
Installation is done via the standard Go package manager:
\n\n# Install the library\ngo get github.com/golang/mock/gomock\n\n# Install the mockgen utility\ngo install github.com/golang/mock/mockgen@v1.6.0\n\nAfter installation, make sure mockgen is available in your PATH. You can verify with the command:
mockgen --version\n\nIf you are using Go 1.16+, it is recommended to add mockgen to tools.go for versioning:
// tools.go\n// +build tools\n\npackage tools\n\nimport (\n _ "github.com/golang/mock/mockgen"\n)\n\n3. Quick Start — Minimal Working Example
\n\nLet's look at a simple example. Suppose we have a Greeter interface that we want to mock.
// greeter.go\npackage main\n\n// Greeter — interface we will mock\ntype Greeter interface {\n Greet(name string) string\n}\n\n// Service uses Greeter\ntype Service struct {\n greeter Greeter\n}\n\nfunc NewService(g Greeter) *Service {\n return &Service{greeter: g}\n}\n\nfunc (s *Service) SayHello(name string) string {\n return s.greeter.Greet(name)\n}\n\nFirst, generate the mock:
\n\nmockgen -source=greeter.go -destination=mock_greeter.go -package=main\n\nNow write the test:
\n\n// greeter_test.go\npackage main\n\nimport (\n "testing"\n "github.com/golang/mock/gomock"\n)\n\nfunc TestSayHello(t *testing.T) {\n // Create a gomock controller\n ctrl := gomock.NewController(t)\n defer ctrl.Finish()\n\n // Create a mock Greeter\n mockGreeter := NewMockGreeter(ctrl)\n\n // Set up expectation: when Greet("Alice") is called, return "Hello, Alice!"\n mockGreeter.EXPECT().Greet("Alice").Return("Hello, Alice!")\n\n // Create a service with the mock\n service := NewService(mockGreeter)\n\n // Call the method under test\n result := service.SayHello("Alice")\n\n // Check the result\n if result != "Hello, Alice!" {\n t.Errorf("Expected 'Hello, Alice!', got '%s'", result)\n }\n}\n\nRun the test:
\n\n
Also in library
Redis client for Go (Golang) — go-redis and radix
Elasticsearch Client for Swift (iOS/macOS): Integration and Examples
Reactive Programming in Go (Golang): Libraries, Patterns, and Examples
Monolog for PHP: Complete Guide with Examples
Watchdog: Real-Time File Monitoring in Python
Recommendations