Using mockery with go generate
This is a simple example of how to use mockery with go generate. It is my preferred way of generating mocks, because the mocks configuration is co-located with the interface definition.
- Install mockery
go install github.com/vektra/mockery/v2@2.23.4 # use latest version
- Add a go:generate comment to your interface
//go:generate mockery -name=MyInterface -output=mocks -outpkg=mocks -case=underscore
type MyInterface interface {
DoSomething()
}
- Run go generate
go generate ./...
Mockery - https://vektra.github.io/mockery/latest/