Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .mockery_gomock.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
template: gomock
structname: "GoMock{{.InterfaceName}}"
filename: "mocks_gomock_{{.SrcPackageName}}_test.go"

all: false
template-data:
typed: True
boilerplate-file: "./.boilerplate.txt"
packages:
github.com/vektra/mockery/v3/internal/fixtures:
interfaces:
Requester:
github.com/vektra/mockery/v3/internal/fixtures/empty_return:
interfaces:
EmptyReturn:
io:
config:
all: True
dir: internal/fixtures/
pkgname: test
5 changes: 5 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ tasks:
cmds:
- MOCKERY_CONFIG=./.mockery_matryer.yml go run .

mocks.generate.gomock:
cmds:
- MOCKERY_CONFIG=./.mockery_gomock.yml go run .

mocks.generate:
desc: generate mocks
deps:
- mocks.generate.mockery
- mocks.generate.matryer
- mocks.generate.gomock

docker:
desc: build the mockery docker image
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (c *RootConfig) GetPackageConfig(ctx context.Context, pkgPath string) (*Pac
// GetPackages returns a list of the packages that are defined in
// the `packages` config section.
func (c *RootConfig) GetPackages(ctx context.Context) ([]string, error) {
packages := []string{}
packages := make([]string, 0, len(c.Packages))
for key := range c.Packages {
packages = append(packages, key)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Test_getFromDB(t *testing.T) {
Why use mockery?
----------------

1. You gain access to a number of pre-curated mock implementations that can be used in testing. This includes traditional "mockery-style" mocks, as well as other styles from the open source community such as from https://github.com/matryer/moq. Such mocks allow you to quickly define how the implementation should behave under test without having to manually curate your own mocks/stubs/fakes.
1. You gain access to a number of pre-curated mock implementations that can be used in testing. This includes traditional "mockery-style" mocks, as well as other styles from the open source community such as from https://github.com/matryer/moq or https://github.com/uber-go/mock. Such mocks allow you to quickly define how the implementation should behave under test without having to manually curate your own mocks/stubs/fakes.
2. Mockery benefits from a large number of performance improvements that almost all other Go code-generation projects currently have not employed. This means that it's orders of magnitude faster for large codebases.
3. Mockery provides a comprehensive, centralized, flexible, and simple configuration scheme driven off of yaml instead of relying on sprawling `//go:generate` commands.
4. Mockery is a code-generation framework. While its original goal is to provide mock implementations for testing purposes, users can supply their own templates to auto-generate any kind of code that needs to be based off of interfaces.
Expand Down
152 changes: 152 additions & 0 deletions docs/template/gomock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
---
title: gomock
---

`gomock` mocks are derived from the project at https://github.com/uber-go/mock.

## Description

=== "Interface"

```go
package test

type Requester interface {
Get(path string) (string, error)
}
```


=== "Example Usage"

```go
package test

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRequesterGoMock(t *testing.T) {
ctrl := gomock.NewController(t)
m := NewGoMockRequester(ctrl)
m.EXPECT().Get("foo").Return("bar", nil).Times(1)
retString, err := m.Get("foo")
assert.NoError(t, err)
assert.Equal(t, "bar", retString)
}
```

=== "`.mockery.yml`"

```yaml
template: gomock
template-data:
typed: true
packages:
github.com/vektra/mockery/v3/pkg/fixtures:
config:
dir: "{{.InterfaceDir}}"
filename: "gomocks.go"
pkgname: "test"
structname: "GoMock{{.InterfaceName}}"
interfaces:
Requester:
```

=== "`mocks_gomock.go`"

```go
// Code generated by mockery; DO NOT EDIT.
// github.com/vektra/mockery
// template: gomock
// source: github.com/vektra/mockery/v3/internal/fixtures (interfaces: Requester)

// Package test is a generated GoMock package.
package test

import (
"reflect"

"go.uber.org/mock/gomock"
)

// GoMockRequester is a mock of Requester interface.
type GoMockRequester struct {
ctrl *gomock.Controller
recorder *GoMockRequesterMockRecorder
isgomock struct{}
}

// GoMockRequesterMockRecorder is the mock recorder for GoMockRequester.
type GoMockRequesterMockRecorder struct {
mock *GoMockRequester
}

// NewGoMockRequester creates a new mock instance.
func NewGoMockRequester(ctrl *gomock.Controller) *GoMockRequester {
mock := &GoMockRequester{ctrl: ctrl}
mock.recorder = &GoMockRequesterMockRecorder{mock}
return mock
}

// EXPECT returns an object that allows the caller to indicate expected use.
func (m *GoMockRequester) EXPECT() *GoMockRequesterMockRecorder {
return m.recorder
}

// Get mocks base method.
func (m *GoMockRequester) Get(path string) (string, error) {
// ...
}

// Get indicates an expected call of Get.
func (mr *GoMockRequesterMockRecorder) Get(path any) *GoMockRequesterGetCall {
// ...
}

// GoMockRequesterGetCall wrap *gomock.Call
type GoMockRequesterGetCall struct {
*gomock.Call
}

// Return rewrite *gomock.Call.Return
func (c *GoMockRequesterGetCall) Return(s string, err error) *GoMockRequesterGetCall {
c.Call = c.Call.Return(s, err)
return c
}

// Do rewrite *gomock.Call.Do
func (c *GoMockRequesterGetCall) Do(f func(string) (string, error)) *GoMockRequesterGetCall {
c.Call = c.Call.Do(f)
return c
}

// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *GoMockRequesterGetCall) DoAndReturn(f func(string) (string, error)) *GoMockRequesterGetCall {
c.Call = c.Call.DoAndReturn(f)
return c
}

```


## `template-data`

`gomock` accepts the following `#!yaml template-data:` keys:

| key | type | description |
|-----|------|-------------|
| `boilerplate-file` | `#!yaml string` | Specify a path to a file that contains comments you want displayed at the top of all generated mock files. This is commonly used to display license headers at the top of your source code. |
| `mock-build-tags` | `#!yaml string` | Set the build tags of the generated mocks. Read more about the [format](https://pkg.go.dev/cmd/go#hdr-Build_constraints). |
| `no-source-comment` | `#!yaml bool` | Disable writing the original package and interface names in a comment. |
| `no-pkg-comment` | `#!yaml bool` | Disable writing a package documentation comment (godoc). |
| `typed` | `#!yaml bool` | Generate type-safe 'Return', 'Do', 'DoAndReturn' functions. |


### Schema

```json
--8<-- "internal/mock_gomock.templ.schema.json"
```
5 changes: 5 additions & 0 deletions docs/template/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Mockery provides a few embedded templates you can render, or you can use a URL t

Mocks generated using this template allow you to define precise functions to be run.

### [`#!yaml template: "gomock"`](gomock.md#description)

[`gomock`](gomock.md#description){ data-preview } templates replicate the mocks generated by `mockgen` from the project at https://github.com/uber-go/mock.

### `#!yaml template: "file://"`

You may also provide mockery a path to your own file using the `file://` protocol specifier. The string after `file://` will be the relative or absolute path of your template.
Expand All @@ -25,6 +29,7 @@ The templates are rendered with the data as shown in the [section below](#templa

You can see examples of how the mockery project utilizes the template system to generate the different mock styles:

- [`gomock.templ`](https://github.com/vektra/mockery/blob/v3/internal/mock_gomock.templ)
- [`matryer.templ`](https://github.com/vektra/mockery/blob/v3/internal/mock_matryer.templ)
- [`testify.templ`](https://github.com/vektra/mockery/blob/v3/internal/mock_testify.templ)

Expand Down
1 change: 1 addition & 0 deletions docs/template/matryer.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ title: matryer
```go
// Code generated by mockery; DO NOT EDIT.
// github.com/vektra/mockery
// template: matryer

package test

Expand Down
1 change: 1 addition & 0 deletions docs/template/testify.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ section below shows what will be rendered for the given interface.
```go
// Code generated by mockery; DO NOT EDIT.
// github.com/vektra/mockery
// template: testify

package test

Expand Down
26 changes: 18 additions & 8 deletions e2e/test_template_exercise/exercise_expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ $typeparam.Constraint.String: golang.org/x/exp/constraints.Integer
# METHOD: 0
$method.Name: Foo
$method.ReturnStatement: return
$method.Call: Foo(ctx, typeParam, ordered)
$method.Call: Foo(ctx, typeParam, ord1, ord2)
$method.AcceptsContext: true
$method.Signature: (ctx context.Context, typeParam T, ordered Ordered) (err error)
$method.SignatureNoName: (context.Context, T, Ordered) (error)
$method.Declaration: Foo(ctx context.Context, typeParam T, ordered Ordered) (err error)
$method.Signature: (ctx context.Context, typeParam T, ord1, ord2 Ordered) (err error)
$method.SignatureNoName: (context.Context, T, Ordered, Ordered) error
$method.Declaration: Foo(ctx context.Context, typeParam T, ord1, ord2 Ordered) (err error)
$method.ReturnsError: true
$method.HasParams: true
$method.HasReturns: true
$method.ReturnArgList: err error
$method.ReturnArgListNoName: error
$method.ArgList: ctx context.Context, typeParam T, ordered Ordered
$method.ArgListNoName: context.Context, T, Ordered
$method.ArgList: ctx context.Context, typeParam T, ord1, ord2 Ordered
$method.ArgListNoName: context.Context, T, Ordered, Ordered


# PARAM: 0
Expand All @@ -100,12 +100,22 @@ $param.MethodArgNoName: T


# PARAM: 2
$param.Var.Name: ordered
$param.Var.Name: ord1
$param.Var.IsSlice: false
$param.Var.Nillable: true
$param.Var.Type.String: Ordered
$param.Var.TypeString: Ordered
$param.MethodArg: ordered Ordered
$param.MethodArg: ord1 Ordered
$param.MethodArgNoName: Ordered


# PARAM: 3
$param.Var.Name: ord2
$param.Var.IsSlice: false
$param.Var.Nillable: true
$param.Var.Type.String: Ordered
$param.Var.TypeString: Ordered
$param.MethodArg: ord2 Ordered
$param.MethodArgNoName: Ordered


Expand Down
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.7
require (
github.com/brunoga/deep v1.2.4
github.com/chigopher/pathlib v0.19.1
github.com/go-viper/mapstructure/v2 v2.2.1
github.com/huandu/xstrings v1.5.0
github.com/jedib0t/go-pretty/v6 v6.6.7
github.com/knadh/koanf/parsers/yaml v0.1.0
Expand All @@ -17,16 +18,18 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.10.0
github.com/xeipuuv/gojsonschema v1.2.0
go.uber.org/mock v0.5.1
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0
golang.org/x/term v0.29.0
golang.org/x/tools v0.31.0
golang.org/x/tools v0.32.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand All @@ -41,10 +44,9 @@ require (
github.com/stretchr/objx v0.5.2 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.22.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,25 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
go.uber.org/mock v0.5.1 h1:ASgazW/qBmR+A32MYFDB6E2POoTgOwT509VP0CT/fjs=
go.uber.org/mock v0.5.1/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM=
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0/go.mod h1:S9Xr4PYopiDyqSyp5NjCrhFrqg6A5zA2E/iPHPhqnS8=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=
golang.org/x/tools v0.32.0 h1:Q7N1vhpkQv7ybVzLFtTjvQya2ewbwNDZzUgfXGqtMWU=
golang.org/x/tools v0.32.0/go.mod h1:ZxrU41P/wAbZD8EDa6dDCa6XfpkhJ7HFMjHJXfBDu8s=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
Loading
Loading