Changes for page Quickstart: 'mockgen' Mocking Library
Last modified by chrisby on 2023/10/18 18:17
Summary
-
Page properties (2 modified, 0 added, 0 removed)
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 walkthroughof 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}} ... ... @@ -24,9 +24,9 @@ 24 24 First, we need the dependency we want to mock, for this example 'NameProvider.go': 25 25 26 26 {{code language="go"}} 27 -package mock_ quickstart28 +package mockgen_example 28 28 29 -//go:generate mockgen -destination=./mock_nameProvider.go -package=mock_ quickstart. NameProvider30 +//go:generate mockgen -destination=./mock_nameProvider.go -package=mockgen_example . NameProvider 30 30 31 31 type NameProvider interface { 32 32 ProvideName() string ... ... @@ -47,7 +47,7 @@ 47 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 48 49 49 {{code language="go"}} 50 -package mock_ quickstart51 +package mockgen_example 51 51 52 52 type Greeter struct { 53 53 nameProvider NameProvider ... ... @@ -62,7 +62,7 @@ 62 62 Finally, here is the test code from 'Greeter_test.go': 63 63 64 64 {{code}} 65 -package mock_ quickstart66 +package mockgen_example 66 66 67 67 import ( 68 68 "github.com/golang/mock/gomock" ... ... @@ -93,6 +93,8 @@ 93 93 94 94 {{code language="bash"}} 95 95 go test ./... 97 +# output should look like this: 98 +# ok example.com/mockgen_example 0.001s 96 96 {{/code}} 97 97 98 98 === ===