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

From version 1.5
edited by chrisby
on 2023/06/18 14:17
Change comment: There is no comment for this version
To version 1.2
edited by chrisby
on 2023/06/17 14:19
Change comment: There is no comment for this version

Summary

Details

Page properties
Tags
... ... @@ -1,1 +1,0 @@
1 -golang|mockgen|mocking
Content
... ... @@ -1,15 +1,14 @@
1 1  === Introduction ===
2 2  
3 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.
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 5  
6 6  
7 7  === Setup ===
8 8  
9 9  {{code language="bash"}}
10 -mkdir -p mockgen_example
11 -cd mockgen_example
12 -go mod init example.com/mockgen_example
10 +mkdir -p mock_quickstart; cd mock_quickstart
11 +go mod init example.com/mocking
13 13  go get github.com/golang/mock/gomock
14 14  go install github.com/golang/mock/mockgen@v1.6.0
15 15  {{/code}}
... ... @@ -25,9 +25,9 @@
25 25  First, we need the dependency we want to mock, for this example 'NameProvider.go':
26 26  
27 27  {{code language="go"}}
28 -package mockgen_example
27 +package mock_quickstart
29 29  
30 -//go:generate mockgen -destination=./mock_nameProvider.go -package=mockgen_example . NameProvider
29 +//go:generate mockgen -destination=./mock_nameProvider.go -package=mock_quickstart . NameProvider
31 31  
32 32  type NameProvider interface {
33 33   ProvideName() string
... ... @@ -48,7 +48,7 @@
48 48  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:
49 49  
50 50  {{code language="go"}}
51 -package mockgen_example
50 +package mock_quickstart
52 52  
53 53  type Greeter struct {
54 54   nameProvider NameProvider
... ... @@ -63,7 +63,7 @@
63 63  Finally, here is the test code from 'Greeter_test.go':
64 64  
65 65  {{code}}
66 -package mockgen_example
65 +package mock_quickstart
67 67  
68 68  import (
69 69   "github.com/golang/mock/gomock"
... ... @@ -94,8 +94,6 @@
94 94  
95 95  {{code language="bash"}}
96 96  go test ./...
97 -# output should look like this:
98 -# ok example.com/mockgen_example 0.001s
99 99  {{/code}}
100 100  
101 101  === ===