Clone this repo:


  1. bd3c8e8 Add extra methods to control the number of times a call is expected. by Daniel Zepeda · 1 year, 4 months ago master
  2. 58a501f Talk about mockgen's reflect mode in README. by Jeff Hodges · 1 year, 6 months ago
  3. 06883d9 mockgen: Support struct{} in types when in source mode. by David Symonds · 1 year, 9 months ago
  4. d581abf Format generated source code before writing it to the destination. by Peter Stace · 1 year, 11 months ago
  5. 15f8b22 Update import paths to be based on by David Symonds · 1 year, 12 months ago

GoMock is a mocking framework for the Go programming language. It integrates well with Go's built-in testing package, but can be used in other contexts too.


Once you have installed Go, run these commands to install the gomock package and the mockgen tool:

go get
go get


After installing, you can use go doc to get documentation:

go doc

Alternatively, there is an online reference for the package hosted on GoPkgDoc here.

Running mockgen

mockgen has two modes of operation: source and reflect. Source mode generates mock interfaces from a source file. It is enabled by using the -source flag. Other flags that may be useful in this mode are -imports and -aux_files.


mockgen -source=foo.go [other options]

Reflect mode generates mock interfaces by building a program that uses reflection to understand interfaces. It is enabled by passing two non-flag arguments: an import path, and a comma-separated list of symbols.


mockgen database/sql/driver Conn,Driver

The mockgen command is used to generate source code for a mock class given a Go source file containing interfaces to be mocked. It supports the following flags:

  • -source: A file containing interfaces to be mocked.

  • -destination: A file to which to write the resulting source code. If you don't set this, the code is printed to standard output.

  • -package: The package to use for the resulting mock class source code. If you don't set this, the package name is mock_ concatenated with the package of the input file.

  • -imports: A list of explicit imports that should be used in the resulting source code, specified as a comma-separated list of elements of the form foo=bar/baz, where bar/baz is the package being imported and foo is the identifier to use for the package in the generated source code.

  • -aux_files: A list of additional files that should be consulted to resolve e.g. embedded interfaces defined in a different file. This is specified as a comma-separated list of elements of the form foo=bar/baz.go, where bar/baz.go is the source file and foo is the package name of that file used by the -source file.

For an example of the use of mockgen, see the sample/ directory. In simple cases, you will need only the -source flag.

TODO: Brief overview of how to create mock objects and set up expectations, and an example.