30 Oct 2022
·
3 min read
·Article 21 / 119
GoHow to Create Unit Tests Using the moq Library in Golang
IH
Ihsan Arif
Writer at Santekno · Backend Engineer
Carrying out unit tests using this mocking method is usually used if several functions have been carried out in interface format so that we can assume that if we call the interface function we believe that it should produce the correct program.
Currently to create mocking we use the library from github.com/matryer/moq . If there is no such library, we can download it first with the command below.
1$ go install github.com/matryer/moq@latestBelow we will try to create a project that has the interface function below.
1package main
2
3import "fmt"
4
5type User struct {
6 Username string `json:"username"`
7 Password string `json:"password"`
8}
9
10//go:generate moq -out main_mock_test.go . UserRepositoryInterface
11type UserRepositoryInterface interface {
12 GetAllUsers() ([]User, error)
13}
14
15type UserService struct {
16 UserRepositoryInterface
17}
18
19func (s UserService) GetUser() ([]User, error) {
20 users, _ := s.UserRepositoryInterface.GetAllUsers()
21 for i := range users {
22 users[i].Password = "*****"
23 }
24 return users, nil
25}
26
27type UserRepository struct{}
28
29func (r UserRepository) GetAllUsers() ([]User, error) {
30 users := []User{
31 {"real", "real"},
32 {"real2", "real2"},
33 }
34 return users, nil
35}
36
37func main() {
38 repository := UserRepository{}
39 service := UserService{repository}
40 users, _ := service.GetUser()
41 fmt.Println(users)
42}Then continue with how to create a unit test which can be seen below
1package main
2
3import (
4 "fmt"
5 "reflect"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/mock"
10)
11
12type UserRepositoryMock struct {
13 mock.Mock
14}
15
16func (r UserRepositoryMock) GetAllUsers() ([]User, error) {
17 args := r.Called()
18 users := []User{
19 {"mock", "*****"},
20 }
21 return users, args.Error(1)
22}
23
24func TestService_GetUser(t *testing.T) {
25 repository := UserRepositoryMock{}
26 repository.On("GetAllUsers").Return([]User{}, nil)
27
28 service := UserService{repository}
29 users, _ := service.GetUser()
30 for i := range users {
31 assert.Equal(t, users[i].Password, "*****", "user password must be encrypted")
32 }
33 fmt.Println(users)
34}
35
36func TestUserService_GetUser(t *testing.T) {
37 type fields struct {
38 UserRepositoryInterface UserRepositoryInterface
39 }
40 tests := []struct {
41 name string
42 fields fields
43 want []User
44 wantErr bool
45 }{
46 {
47 name: "case ambil data user",
48 fields: fields{
49 UserRepositoryInterface: UserRepositoryMock{},
50 },
51 },
52 }
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 s := UserService{
56 UserRepositoryInterface: tt.fields.UserRepositoryInterface,
57 }
58 got, err := s.GetUser()
59 if (err != nil) != tt.wantErr {
60 t.Errorf("UserService.GetUser() error = %v, wantErr %v", err, tt.wantErr)
61 return
62 }
63 if !reflect.DeepEqual(got, tt.want) {
64 t.Errorf("UserService.GetUser() = %v, want %v", got, tt.want)
65 }
66 })
67 }
68}Related Articles
Go
29 Jul 2026
Task Breakdown: Splitting a Golang Spec into Executable Work Units
16 mnt
Read
Go
28 Jul 2026
Plan Mode in Claude Code: Build a Solid Golang Implementation Plan
17 mnt
Read
Go
27 Jul 2026
Non-Functional Specifications: Performance, Security, Scalability
16 mnt
Read
Go
24 Jul 2026
Your First OpenAPI Spec with Claude: Contract-First API Design
15 mnt
Read