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.

  1. Install mockery
go install github.com/vektra/mockery/v2@2.23.4 # use latest version
  1. Add a go:generate comment to your interface
//go:generate mockery -name=MyInterface -output=mocks -outpkg=mocks -case=underscore
type MyInterface interface {
    DoSomething()
}
  1. Run go generate
go generate ./...

Mockery - https://vektra.github.io/mockery/latest/