Back to course

Mocking with Interfaces

Go (Golang) for Cloud-Native Microservices

Mocks and Fakes

Since we use interfaces for our repositories, we can easily create a MockRepository for our tests. This avoids hitting a real database during unit tests.

go type MockRepo struct {} func (m *MockRepo) GetByID(id int) (*User, error) { return &User{ID: id, Name: "Test User"}, nil }

This makes your tests fast and deterministic.