I am struggling to understand mocking in Go (am looking for something related to Mockito.spy equivalent of java in Go).
Let's say I have an interface in Go with 5 methods. But the piece of code that I want to test has references to only two methods. Now how do I mock this dependency without implementing all methods , i.e my actual implementation in source code implements 5 methods of interface , but is there a way to avoid implementing dummy interface implementation of 5 methods in test file. Following is the way am currently doing , implementing 5 methods is manageable but what if the interface has 20 methods , it becomes tedious to mock implement all the methods in test file.
Example:
Source code in handler.go:
type Client struct {}
type ClientStore interface {
func(c *Client) methodOne() error {// some implementation}
func(c *Client) methodTwo() error {// some implementation}
func(c *Client) methodThree() error {// some implementation}
func(c *Client) methodFour() error {// some implementation}
func(c *Client) methodFive() error {// some implementation}
}
Source code in api.go:
func processFeed(c Client) error {
err := c.methodOne()
if(err != null) {
return err
}
err1 := c.methodTwo()
if(err1 != null) {
return err1
}
}
Test class code:
import "testify/mock"
func TestFeed(t *testing.T){
mockClient := &MockClient{}
err := processFeed(mockClient)
assert.NotNil(t , err)
}
type MockClient struct {
mock.Mock
}
func(c *MockClient ) methodOne() error {fmt.Printf("methodOne");nil}
func(c *MockClient ) methodTwo() error {return errors.New("mocked error")}
func(c *MockClient ) methodThree() error {fmt.Printf("methodThree");nil}
func(c *MockClient ) methodFour() error {fmt.Printf("methodFour");nil}
func(c *MockClient ) methodFive() error {fmt.Printf("methodFive");nil}
Question:
Is there a way to mock only what i required in the above case only the methodOne() and methodTwo() methods and not worry about remaining methods in tests ? Can you please suggest any other alternatives if they are present ? Thank you