Version 1.1 by chrisby on 2023/06/17 14:18

Show last authors
1 === Introduction ===
2
3 (% style="text-align: justify;" %)
4 In contrast to run-time mock generation, as seen 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 walkthrough 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 First, we need the dependency we want to mock, for this example 'NameProvider.go':
24
25 {{code language="go"}}
26 package mock_quickstart
27
28 //go:generate mockgen -destination=./mock_nameProvider.go -package=mock_quickstart . NameProvider
29
30 type NameProvider interface {
31 ProvideName() string
32 }
33 {{/code}}
34
35 (% style="text-align: justify;" %)
36 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:
37
38 {{code}}
39 go generate ./...
40 {{/code}}
41
42 The 'mock_nameProvider.go' file should now be generated for the appropriate path and package.
43
44 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:
45
46 {{code language="go"}}
47 package mock_quickstart
48
49 type Greeter struct {
50 nameProvider NameProvider
51 }
52
53 func (n *Greeter) Greet() string {
54 return "Hello " + n.nameProvider.ProvideName()
55 }
56 {{/code}}
57
58 Finally, here is the test code from 'Greeter_test.go':
59
60 {{code}}
61 package mock_quickstart
62
63 import (
64 "github.com/golang/mock/gomock"
65 "testing"
66 )
67
68 func TestGreet(t *testing.T) {
69 ctrl := gomock.NewController(t)
70 nameProviderMock := NewMockNameProvider(ctrl)
71 defer ctrl.Finish()
72 greeter := Greeter{nameProviderMock}
73
74 nameProviderMock.EXPECT().ProvideName().Return("John")
75
76 greeting := greeter.Greet()
77 if greeting != "Hello John" {
78 t.Fail()
79 }
80 }
81 {{/code}}
82
83 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.
84
85 === ===
86
87 === Running the Test ===
88
89 {{code language="bash"}}
90 go test ./...
91 {{/code}}
92
93 === ===
94
95 === Ignore Mock Files in Git ===
96
97 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:
98
99 {{code}}
100 **/mock_*.go
101 {{/code}}