How to Mock Interfaces in Go

Create a struct that implements the interface methods and inject it to replace the real dependency during testing.

Mock interfaces in Go by defining a struct that implements the interface and injecting it into your code instead of the real implementation.

type MockHandler struct {
    Handler
    err error
}

func (h *MockHandler) Handle(ctx context.Context, r Record) error {
    return h.err
}

// Usage
mock := &MockHandler{err: errors.New("mock failing")}
logger := New(mock)