Framework
uber-go/mock avatar
uber-go/mock

uber-go/mock: the fork that changed the import path, so nothing about it is drop-in

GoMock is a mocking framework for the Go programming language.

3,432 stars184 forksGoApache-2.0

At a glance

What is it?
Uber's maintained fork of the mocking library that Google stopped updating, published under a new module path, with the type-safe generated API switched off by default and a documented way to generate an import cycle by accident. The readme's own examples contain a one-second sleep.
Who is it for?
Adopt it if you are starting a Go project with mocks in it, because the maintainers here are shipping releases and the upstream is not. Do not expect to swap it into an existing codebase for free, because the module path changed and every import and every generation directive has to be rewritten.
Can I use it commercially?
Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 82 days ago.
What is it written in?
Mainly Go, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 15, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • uber-go/mock3,432 ★ · +19
Star history
Repository2026-09-242026-11-15
uber-go/mock3.4k3.4k

19 Stars in the last 52 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The fork changed the import path, so nothing here is drop-in

The single most consequential line in the file is the fork notice: this project originates from Google's mocking repository, Google no longer maintains it, and given heavy internal use the decision was made to fork and maintain it at Uber. The module declaration is where the cost lands, because the import path is no longer the one almost every existing Go project has in its source tree:

code
module go.uber.org/mock

go 1.25.0

A migration is therefore not a version bump. Every import of the framework, every import of the generator, every go:generate line, and every tooling configuration that names the old path has to be rewritten, and the old and new packages are different enough that you cannot have both in one build without renaming one of them. The repository, the module and the package all carry different names too: the repository is under the Uber organisation, the module path uses a domain prefix rather than a hosting path, and the directory holding the library is the older name, with the readme's own title still being the original project's name. The fork kept the parent project governance files as well, an authors file and a maintainers file at the root, which is the convention the original repository used and is not something a fork has to adopt.

The supported Go versions are stated as a moving target

Rather than naming versions, the file states a policy: the library supports all Go versions supported by the official release policy, which it then glosses as the two most recent releases of Go. That is a reasonable thing to promise for a library and an awkward thing to consume, because the promise is defined by a document outside the repository and changes without notice. The module file expresses the same idea as a number, and the number is higher than the promise. A module requiring 1.25.0 cannot be consumed by a project pinned below that, whatever the release policy says about support. So the two statements, one of them prose and one of them enforced by the toolchain, do not have the same weight, and the enforced one is the one that fails your build.

The Bazel rules are released on a version line of their own

The release list interleaves two different products. Three library releases are on one line, with the newest at 0.6.0 from August 2025. Two of the entries are rule releases with a prefixed name and their own version numbers, 0.1.0 and 0.2.0, both from June 2025, and the second of them carries a human title where the first carries a machine one. That is not an accident of naming. The tree has a directory for the Bazel definitions, separate from the library and the generator, so a Bazel consumer pins two versions that do not move together: taking the newest library release does not give you the newest rule. Publishing is handled by a release tool with its own configuration file at the root, and there is a directory for CI configuration and another for tool dependencies.

Type-safe generated code is off unless you ask for it

The flag list is the most useful part of the documentation, and one entry stands out:

code
- `-typed`: Generate Type-safe 'Return', 'Do', 'DoAndReturn' function. (default false)

So the default output is the older style, where the generated call chain reaches the mocked method's return values through reflection, and the compile-time-safe variant has to be requested. The flag matters more than a normal default: the typed form fails at compile time when a test's stub no longer matches the interface, while the untyped form fails at run time, inside the test, after the code under test has already been exercised. Two neighbouring defaults lean the other way. Package documentation and the source comment are written by default, so generated files arrive annotated, while the regeneration directive is not, so nothing in the output tells a future reader how it was produced.

Writing to standard output is the case that can generate an import cycle

There is a flag whose documentation is a short post-mortem of a bug, and it is the most practically useful paragraph in the file. The flag names the import path for the generated code, and its purpose is to stop the generator producing a file that imports itself. The explanation of when that happens is precise: it occurs when the mock's package is set to one of the inputs, usually the main one, and the output goes to standard output, so the generator cannot detect where the result will land. The obvious consequence is that printing to standard output and writing to a file inside the project are not equivalent here, and the flag is what makes the first case workable. The destination flag documents the same choice more gently, noting only that without it the code is printed rather than written.

Generated package names begin with mock_

The package flag defaults to the literal string mock_ concatenated with the package name of the input file. That produces package identifiers with a connector character in the middle, which is not how Go package names are conventionally written and which reads oddly in an import list. The naming is at least predictable, and predictability is what matters for a generated file that a human will read in a diff, but the default could have been a separator that does not violate the usual conventions. The related naming flag lets you override both ends of it: a list mapping interface names to the mock names you want, in which the factory function and the recorder are named after the mock, with the default convention applying to any interface you leave out.

The generator has three modes and only two of them are usable from a file path

The tool offers archive, source and package modes, and the split between them is worth understanding before you write build integration. Archive mode takes a compiled package archive and a comma-separated list of symbols, so it needs the package built first; the file's own example builds an archive for the database driver package and then names two interfaces from it. Source mode reads a file and adds two supporting flags for resolving embedded interfaces declared elsewhere. Package mode is the one designed for automation, taking an import path and a symbol list, with a shorthand for the current directory that the file notes is convenient for a generation directive. The import list flag exists because generated files sometimes need explicit renames, in the form of one name mapped to a path. Two flags exist for the edges of the job rather than the middle of it. One prints the parser results and generates nothing, which is the flag to reach for when the tool cannot find an interface you can see plainly in the source. The other names a copyright file whose contents are placed at the top of the generated output, so a generated mock in a repository with a licence header does not have to be the one file without one. The sample tree in the repository is arranged to exercise this machinery rather than to look pretty: four separate import packages, numbered in sequence, plus a concurrency example and a mock test, each one a different shape for the generator to resolve.

The stub example sleeps for a second, and the failure text is the best part

Two details in the examples are worth flagging. The stub example, which is the copy-paste path for anyone who wants a mock that returns values without asserting, sleeps one second inside the function it hands to the generator, and then allows the call any number of times. A reader who copies that example into a test suite has copied a slow test. The other detail is an improvement rather than a problem: when a matcher fails, the tool prints the received value against the expected one, and both halves are overridable. The expected text comes from a matcher's string method, and the received text from the object's own string method when it has one. Both are replaceable through a formatter and a stringer function, and the file shows rewriting one matcher's wording. For a library whose whole job is producing failure messages, making that text a designed surface rather than a fixed string is the right call. The worked example of that output in the file shows a slice received against a slice expected, then the call location, then the argument index that did not match, so a failing test tells you which call, which argument and which position before you start reading the diff.

Editorial conclusion

Adopt it if you are starting a Go project with mocks in it, because the maintainers here are shipping releases and the upstream is not. Do not expect to swap it into an existing codebase for free, because the module path changed and every import and every generation directive has to be rewritten. Four things to decide before you generate anything. Turn on the type-safe generator flag if you can, since the default output relies on reflection where the typed output does not. Set a destination file rather than writing to standard output, because that is the case the tool documents as able to produce an import cycle it cannot see. Expect to write your own regeneration directive, since that is off by default while the other two write options are on. And if you need Bazel, note the rule set is released on its own version line, so the two versions in your dependency graph move independently.

Frequently asked questions

What are the differences between mockery and gomock?

This repository does not compare itself to mockery, so it cannot answer that. What it documents about itself: it integrates with Go's built-in testing package, generates mocks from an interface description with a code generator that has archive, source and package modes, provides a controller, expectations and matchers, and allows the failure text for both the received and expected values to be rewritten.

What is the difference between Uber and Uber Go?

The project makes no such distinction. It is a fork of a Google repository that Google stopped maintaining, taken over because of heavy use inside Uber. The module path is go.uber.org/mock rather than the original hosting path, which is the practical difference that matters when you are migrating an existing codebase.

How do I install the mockgen tool?

Install it with go install at the latest version, then confirm it with mockgen -version. If that fails, the file says to make sure your GOPATH bin directory is on your path, and gives the export line that adds it. The library itself is a normal module dependency and the generator is a separate binary.

Does uber-go/mock generate type-safe mocks by default?

No. There is a flag for generating type-safe return, do and do-and-return functions and its documented default is false, so the default output uses the reflection-based style. Enabling it moves failures from run time to compile time. Two other write options are on by default: the package documentation comment and the source or interface names comment.

Official sources

  1. Issues
  2. License: Apache-2.0
  3. README
  4. Releases
  5. uber-go/mock on GitHub
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/uber-go-mock.svg)](https://hysenlabs.com/projects/uber-go-mock)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/uber-go-mock.svg)](https://hysenlabs.com/projects/uber-go-mock)

Was this useful?