Version 1.3 by chrisby on 2023/06/17 14:21

Show last authors
1 === Introduction ===
2
3 (% style="text-align: justify;" %)
4 In contrast to run-time mock generation, as seen for example in Java's 'mockito' library, Go uses a unique compile-time strategy. This technique results in the creation of tangible mock files that are directly integrated into the test code. The 'mockgen' library automates this process, providing an efficient means of simulating and handling dependencies during testing. This quick start guide provides a basic introduction of the 'mockgen' library, outlining the necessary steps for initial mock generation and its integration into the test workflow.
5
6
7 === Setup ===
8
9 {{code language="bash"}}
10 mkdir -p mock_quickstart; cd mock_quickstart
11 go mod init example.com/mocking
12 go get github.com/golang/mock/gomock
13 go install github.com/golang/mock/mockgen@v1.6.0
14 {{/code}}
15
16 (% style="text-align: justify;" %)
17 The last command installs the 'mockgen' in 'GOPATH', which does the actual mock file generation.
18
19 === ===
20
21 === Source Code ===
22
23 (% style="text-align: justify;" %)
24 First, we need the dependency we want to mock, for this example 'NameProvider.go':
25
26 {{code language="go"}}
27 package mock_quickstart
28
29 //go:generate mockgen -destination=./mock_nameProvider.go -package=mock_quickstart . NameProvider
30
31 type NameProvider interface {
32 ProvideName() string
33 }
34 {{/code}}
35
36 (% style="text-align: justify;" %)
37 Go uses what are called 'build constraints' such as '~/~/go:generate ...' which execute special commands when the application is built. In this case, when 'go generate' is executed, the just installed 'mockgen' command is also executed to create the mock file. The parameters are intuitive: 'destination' defines the path and name of the mock file, 'package' defines the package to address when importing and using the mock file, the dot means the current directory and 'NameProvider' is the type of mock object. Now the mock file can be created by running it:
38
39 {{code}}
40 go generate ./...
41 {{/code}}
42
43 (% style="text-align: justify;" %)
44 The 'mock_nameProvider.go' file should now be generated for the appropriate path and package.
45
46 (% style="text-align: justify;" %)
47 The unit to be tested, 'Greeter', is simple for the sake of an example. It has a dependency of type 'NameProvider' into which the mock object can be injected, and its method 'Greet()' adds a "Hello " before the name provided by the 'NameProvider'. The code for 'Greeter.go' is:
48
49 {{code language="go"}}
50 package mock_quickstart
51
52 type Greeter struct {
53 nameProvider NameProvider
54 }
55
56 func (n *Greeter) Greet() string {
57 return "Hello " + n.nameProvider.ProvideName()
58 }
59 {{/code}}
60
61 (% style="text-align: justify;" %)
62 Finally, here is the test code from 'Greeter_test.go':
63
64 {{code}}
65 package mock_quickstart
66
67 import (
68 "github.com/golang/mock/gomock"
69 "testing"
70 )
71
72 func TestGreet(t *testing.T) {
73 ctrl := gomock.NewController(t)
74 nameProviderMock := NewMockNameProvider(ctrl)
75 defer ctrl.Finish()
76 greeter := Greeter{nameProviderMock}
77
78 nameProviderMock.EXPECT().ProvideName().Return("John")
79
80 greeting := greeter.Greet()
81 if greeting != "Hello John" {
82 t.Fail()
83 }
84 }
85 {{/code}}
86
87 (% style="text-align: justify;" %)
88 The first four lines of the test are just to create the mock object and inject it into 'greeter'. Next, the mock can be instructed to behave in a specific way appropriate to the test case, i.e. to simply return "john" when "ProvideName()" is called. Note that the test calls the 'NewMockNameProvider()' method directly from the 'mock_nameProvider.go' file.
89
90 === ===
91
92 === Running the Test ===
93
94 {{code language="bash"}}
95 go test ./...
96 {{/code}}
97
98 === ===
99
100 === Ignore Mock Files in Git ===
101
102 (% style="text-align: justify;" %)
103 Generated code should not be part of the git history, and should therefore be ignored. Using the mock naming convention from above, a corresponding entry in the '.gitignore' file could be:
104
105 {{code}}
106 **/mock_*.go
107 {{/code}}