Last modified by chrisby on 2023/10/18 18:17

From version 1.1
edited by chrisby
on 2023/06/17 14:18
Change comment: There is no comment for this version
To version 1.7
edited by chrisby
on 2023/10/18 18:17
Change comment: Update document after refactoring.

Summary

Details

Page properties
Tags
... ... @@ -1,0 +1,1 @@
1 +golang|mockgen|mocking
Content
... ... @@ -1,14 +1,15 @@
1 1  === Introduction ===
2 2  
3 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.
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 5  
6 6  
7 7  === Setup ===
8 8  
9 9  {{code language="bash"}}
10 -mkdir -p mock_quickstart; cd mock_quickstart
11 -go mod init example.com/mocking
10 +mkdir -p mockgen_example
11 +cd mockgen_example
12 +go mod init example.com/mockgen_example
12 12  go get github.com/golang/mock/gomock
13 13  go install github.com/golang/mock/mockgen@v1.6.0
14 14  {{/code}}
... ... @@ -20,12 +20,13 @@
20 20  
21 21  === Source Code ===
22 22  
24 +(% style="text-align: justify;" %)
23 23  First, we need the dependency we want to mock, for this example 'NameProvider.go':
24 24  
25 25  {{code language="go"}}
26 -package mock_quickstart
28 +package mockgen_example
27 27  
28 -//go:generate mockgen -destination=./mock_nameProvider.go -package=mock_quickstart . NameProvider
30 +//go:generate mockgen -destination=./mock_nameProvider.go -package=mockgen_example . NameProvider
29 29  
30 30  type NameProvider interface {
31 31   ProvideName() string
... ... @@ -39,12 +39,14 @@
39 39  go generate ./...
40 40  {{/code}}
41 41  
44 +(% style="text-align: justify;" %)
42 42  The 'mock_nameProvider.go' file should now be generated for the appropriate path and package.
43 43  
47 +(% style="text-align: justify;" %)
44 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 45  
46 46  {{code language="go"}}
47 -package mock_quickstart
51 +package mockgen_example
48 48  
49 49  type Greeter struct {
50 50   nameProvider NameProvider
... ... @@ -55,10 +55,11 @@
55 55  }
56 56  {{/code}}
57 57  
62 +(% style="text-align: justify;" %)
58 58  Finally, here is the test code from 'Greeter_test.go':
59 59  
60 60  {{code}}
61 -package mock_quickstart
66 +package mockgen_example
62 62  
63 63  import (
64 64   "github.com/golang/mock/gomock"
... ... @@ -80,6 +80,7 @@
80 80  }
81 81  {{/code}}
82 82  
88 +(% style="text-align: justify;" %)
83 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 84  
85 85  === ===
... ... @@ -88,12 +88,15 @@
88 88  
89 89  {{code language="bash"}}
90 90  go test ./...
97 +# output should look like this:
98 +# ok example.com/mockgen_example 0.001s
91 91  {{/code}}
92 92  
93 -=== ===
101 +=== ===
94 94  
95 95  === Ignore Mock Files in Git ===
96 96  
105 +(% style="text-align: justify;" %)
97 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 98  
99 99  {{code}}