How To Mock A Kitex Client
Introduction
Sometimes, during testing, it may not be necessary to create an actual Kitex client. Instead, a mock client can be used as a stub object. This article will explain how to combine go mock to achieve this.
Usage
Installing go mock (mockgen)
For more details, please refer to: go mock
Using go mock
The mock approach for Kitex is as follows:
Locate the client in the kitex_gen directory, which contains the corresponding client interface. Use go mock to generate a mock client.
This command will generate client_mock.go, which can be used in tests:
Others
If you don’t want to use go mock or if you want to verify other capabilities of the client (such as tracing), you can also specify custom middleware when creating the client to construct data as needed.
Note: The built-in middleware of the Kitex client will still be executed.