gomock for Go: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

gomock for Go: Complete Guide to Interface Mocking

\n\n

1. What It Is and Why You Need It

\n\n

In 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\n

gomock 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.

\n\n

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\n

2. Installation

\n\n

To work with gomock, you need to install two components:

\n\n
    \n
  1. gomock library — contains the runtime for working with mocks.
  2. \n
  3. mockgen utility — a code generator for mocks from interfaces.
  4. \n
\n\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\n

After installation, make sure mockgen is available in your PATH. You can verify with the command:

\n\n
mockgen --version
\n\n

If you are using Go 1.16+, it is recommended to add mockgen to tools.go for versioning:

\n\n
// tools.go\n// +build tools\n\npackage tools\n\nimport (\n    _ "github.com/golang/mock/mockgen"\n)
\n\n

3. Quick Start — Minimal Working Example

\n\n

Let's look at a simple example. Suppose we have a Greeter interface that we want to mock.

\n\n
// 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\n

First, generate the mock:

\n\n
mockgen -source=greeter.go -destination=mock_greeter.go -package=main
\n\n

Now 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\n

Run the test:

\n\n

  
  

Recommendations