01 / 04

How do you mock dependencies in Go without a mocking framework?

Define interfaces for dependencies and implement them with simple structs in tests that return preset values or record calls. This is idiomatic Go and requires no reflection.

Hand-written mocks
When to use generated mocks
  1. 1

    Hand-written mocks: preferred for small interfaces (1-3 methods) — simple and readable

  2. 2

    testify/mock: adds call expectations and assertion helpers for complex interaction testing

  3. 3

    gomock (mockgen): generates mocks from interfaces — useful for large interfaces with many methods

  4. 4

    Avoid over-mocking: if your test mocks everything, it tests nothing — prefer integration tests for complex scenarios

  5. 5

    Functional options on mocks: allow test-specific behavior without modifying the mock struct