unit-testinggomockingtestifygomock

How to mock only one method of an interface


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


Solution

  • First, if your interface has 5 methods, and you're only using one, your interface is too big. Use a smaller interface.

    type BigInterface interface {
        Thing1()
        Thing2()
        ThingN()
    }
    
    type SmallInterface interface {
        Thing1()
    }
    
    func MyFunc(i SmallInterface) { ... }
    

    Another option is to create a complete implementation of the full interface, by embedding the full interface. This will panic if you try to access one of the other methods, but will work for testing if you're careful. (But please don't do this in production code!)

    type BigInterface interface {
        Thing1()
        Thing2()
        ThingN()
    }
    
    type myImplementation struct {
        BigInterface
    }
    
    func (i *myImplementation) Thing1() { ... }
    

    Now myImplementation satisfies the BigInterface interface, by virtue of containing an embedded instance of BigInterface. If you never set that embedded instance to anything, then calling those methods will panic, but you can still define Thing1 to do what you want for your tests.