test double
Any object that replaces a real dependency in a test: a stub returns canned data, a fake is a small working implementation, and a mock checks interactions.
In more detail
The names matter less than the signal you need. Keep the replacement simpler than the real thing and make its assumptions visible in the test.
Examples
Go: any type with the right methods satisfies a small interface, so doubles are cheaptype fake struct{}
func (fake) RoundTrip(*http.Request) (*http.Response, error) { ... }Go: a fake http.RoundTripper returns a fixed response with no networkIn this course: There are four honest reasons to use one: the real thing is slow, not yours to call, not deterministic, or cannot be made to fail on demand. Making a test easier to write is not one of them. The term and the five kinds come from Gerard Meszaros's book xUnit Test Patterns.Related terms
- mock — A test double that stands in for a real dependency and checks that the code under test calls it in the expected way.
- stub — A stand-in for a dependency in a test that simply returns prepared answers, without checking how it was called.
- unit test — An automated check of one small piece of code, such as a single function, that runs quickly and on its own.
- NSubstitute — A .NET library that creates test doubles for interfaces with readable syntax, such as Substitute.For<IProductRepository>().
- dependency injection — Giving an object the collaborators it needs from outside, usually through its constructor, instead of letting it create or look them up itself.
- test fixture — Fixed, known data or setup that tests use so their results are predictable, such as a sample file, a seeded database or a started application.
- implicit interface satisfaction — Go's rule that a type meets an interface just by having the right methods, with no implements declaration.
- integration test — A test that checks several real parts working together, such as the service talking to a real database.
- interface (Java) — A Java type that lists operations without fixing how they are carried out, so different classes can provide their own implementations.
- test spy — A test double that records how a function was called, with its arguments and results, so a test can check afterwards how it was used.
- fake — A test double that is a small but working implementation of a dependency, such as an in-memory database standing in for a real one.
- dummy — A placeholder value passed only to satisfy a function's parameter list and never actually used.
- collaborator — Another object or service that the code under test calls to get its work done, such as a payment gateway or a clock.
- seam — A place in the code where you can swap in different behaviour without editing the code itself, such as a parameter.
Taught in
- Mocking Dependencies — Modern API Architecture: REST, GraphQL & gRPC
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- The Repository Pattern — Modern .NET Core, C# & Enterprise Microservices
- Unit Testing with xUnit and NSubstitute — Modern .NET Core, C# & Enterprise Microservices
- Integration Testing with WebApplicationFactory — Modern .NET Core, C# & Enterprise Microservices
- Interfaces and Why They Are Implicit — Concurrent Systems Programming with Go
- The Standard testing Package — Concurrent Systems Programming with Go
- Calling APIs with net/http Client — Concurrent Systems Programming with Go
- Designing Testable Code — Modern Java: Virtual Threads & High-Throughput Services
- Dependency Injection And The IoC Container — Modern Java: Virtual Threads & High-Throughput Services
- Configuration And Profiles — Modern Java: Virtual Threads & High-Throughput Services
- Testing APIs With MockMvc — Modern Java: Virtual Threads & High-Throughput Services
- Database Integration With Testcontainers — Modern Java: Virtual Threads & High-Throughput Services
- Tying It All Together — Modern Java: Virtual Threads & High-Throughput Services
- What Test Doubles Are For — Automated Testing, TDD & Quality Engineering
- Fakes, Stubs, Mocks And Spies — Automated Testing, TDD & Quality Engineering
- Why Over-Mocking Breeds False Positives — Automated Testing, TDD & Quality Engineering