Mocking with mockgen

Utkarsh Mani Tripathi
3 min readMay 1, 2021

Unit testing is pretty interesting topic and it helps you in writing good code. It gives you a confidence while building your software but most of the times developers ignores this because it is time consuming. Recently i learned about a very good tool mockgen, which is being used widely in the Go community for mocking purpose. In this blog, i will cover how to mock the behaviours of your program using mockgen.

Mocking with mockgen

Need of unit testing arises when project size grows day by day and hence the probability of making logical mistakes. Mocking is helpful in unit testing to focus on testing the business logic and fake the behaviour or state of the external dependencies such as database, third party libraries, networking etc.

Prerequisits

Let’s look at the basic code which inserts a row to the database table. You can take a look at the complete code here.

And there is another file which calls the Insert() function.

Generate Mocks

To test insertUser function, you will need to setup the database like it is done in main function which is what we want to avoid in case of unit testing. Now to mock the database call or user.Insert(), we will need to generate the mock file using mockgen tool. Just change the directory where users.go resides and run following command.

mkdir mocks
mockgen . Users >> mocks/mock_users.go

Here first we have created a directory as mockgen doesn’t create itself and in the next step we pass the current directory (.) and interface name Users as argument to the mockgen and redirect the generated output to mock_users.go file. (You can pass several interfaces separated by comma which exists in the same directory)

Testing your changes

Now to test the insertUser() function you will need to make following changes to your test code:

  • Create an instance of gomock.Controller and don’t forget to add defer mockCtl.Finish()
  • Create and pass MockUsers instance while calling insertUser() function.

Note:

  • Times() is used to exhibit the behaviour of the mock for given number of times. You can also use AnyTimes() for simulating the behaviour any number of times.
  • If you want to return some specific outputs from your mocked function in order, you can wrap the expected behaviour inside gomock.Order() . For exp: I want my mocked function to return error in first call and return 5 in the second call, so i have wrapped them accordingly. This is helpful if you want to mock your functions which are called inside a loop or similar cases.
  • You can use go generate to automate the same by adding following lines just below package statement and run go generate
  • -destination flag where you want to put your mock code; -package to specify the package name of the generated code and then you need to provide the path for which the mocks needs to be generated.

Mockgen makes your job easy and do pretty much everything for you. I am afraid that one day we will have some tools in place which will even implement test cases for us and take away our job 😄, but until we have that tool arrives in this world, you can happily use mockgen to complete your work.

I hope you have learned how to use mockgen from my blog, and in case you have any questions/doubts or suggestions just comment below and i will try to answer with the best of my knowledge.

That’s all folks! As always thanks for reading. 😊

~ उत्कर्ष_उवाच

--

--