In this article I will demonstrate how to use the golang/gomock project to generate mock implementations of interfaces. The mock implementations will be used to inject behavior into the dependencies of a Go app for testing.
Recall from the previous article that we are developing an API to manage Contacts. In Go API Development: Part 1 we created a main application entry point that binds various API routes to HTTP handlers defined by an HTTP controller, and we wired up the controller with the interfaces it needs to do it’s job. We also implemented a service struct that implements the ContactsManager interface.
In this article we will implement the ContactsDataAccessor interface so that we can start to actually persist the Contacts that we manage. We will implement this interface using sqlite as our database backend.
This is the first article in a series of articles on developing a web API using Golang. In this series I will walk you through developing a simple REST API in Go using the Service Pattern with complete examples of Dependency Injection, Mocking, and a simple Redis Caching layer. We’ll be using sqlite as our database backend.