GoMock is an official mocking framework from Google for generating mock objects from Go interfaces. Note: This project is deprecated as of 2023, but it's still widely used in existing codebases.
GoMock has been officially deprecated. Consider using alternatives like:
go get github.com/golang/mock/gomock
go install github.com/golang/mock/mockgen@latest- Auto-generation: Generate mocks from interfaces
- Expectation Setting: Define expected method calls
- Argument Matching: Flexible argument matchers
- Call Ordering: Verify call sequences
- Integration: Works with Go's testing package
# From interface in file
mockgen -source=db.go -destination=mock_db.go -package=mocks
# From package
mockgen -destination=mock_db.go package/path InterfaceNamefunc TestUserService(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockDB := mocks.NewMockUserRepository(ctrl)
mockDB.EXPECT().GetUser(1).Return(&User{Name: "John"}, nil)
// Use mock in tests
}# Generate mocks first
go generate ./...
# Then run tests
go test- ✅ Official Google project
- ✅ Automatic mock generation
- ✅ Type-safe mocks
- ✅ Comprehensive expectation system
- ❌ Deprecated since 2023
- ❌ More verbose than alternatives
- ❌ Requires separate generation step
- ❌ Limited to interfaces only
- Use go:generate: Automate mock generation
- Consider alternatives: Mockery or Testify for new projects
- Verify expectations: Always call
ctrl.Finish() - Use argument matchers: For flexible expectations