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)
Mocking an interface means creating a fake version of a component that behaves exactly like the real one but returns predictable results. You use this to test your code in isolation without relying on external systems like databases or network services. It is like using a rubber duck to practice a conversation instead of talking to a real person.