Test doubles
A test double is a stand-in for a collaborator in a test. Dummies, stubs, spies, mocks, and fakes are different jobs. The word "mock" is often used for all of them, which hides those jobs.
Detailed Explanation
The class under test talks to a mailer, a clock, a repository. The real ones are slow or have side effects. A double replaces them.
- Dummy: passed but never used (a required constructor arg).
- Stub: returns canned data (
repo.findalways returnsuser). - Spy: a real object with some methods stubbed or with recorded calls.
- Mock: a double whose interactions you assert (
verifysend was called once). - Fake: a working slim implementation (an in-memory map as a repository).
Gerard Meszaros named these. Mockito "mocks" are often stubs plus optional verification. A fake is usually hand-written and more stable for collections and repos: MapUserRepo behaves like a repo, so you test rules, not call counts.
You reach for a fake when the collaborator has a simple contract you can implement. You reach for a stub when one return value is enough. You reach for a mock when the outgoing message is the requirement (an event must be published). You reach for a dummy when the compiler wants a parameter.
The cost of mocks everywhere is brittle tests. The cost of a fake that drifts from the real contract (unique keys, null rules) is a green test and a red production. Keep fakes honest. The cost of a spy on a half-real object is confusion about what ran.
Do not double types you do not own if you can avoid it (HTTP clients): wrap them in your port and fake the port. Do not double value objects.
Pick the double from the question the test asks. "Does welcome send the right address?" is a mock or a recording fake. "Does findByEmail treat unknown as empty?" is a fake repo with two rows. Using a mock for the second question leads to stubbing Optional.empty() and never testing the real lookup rule. Using a fake for the first question is fine if the fake records calls. The names exist so you do not default to Mockito for every collaborator.
Code Examples
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
interface Users {
Optional<String> name(String id);
}
class MapUsers implements Users { // fake
private final Map<String, String> data = new HashMap<>();
void put(String id, String name) {
data.put(id, name);
}
@Override
public Optional<String> name(String id) {
return Optional.ofNullable(data.get(id));
}
}
class Greeting {
private final Users users;
Greeting(Users users) {
this.users = users;
}
String hello(String id) {
return "hi " + users.name(id).orElse("guest");
}
public static void main(String[] args) {
MapUsers users = new MapUsers();
users.put("1", "Ana");
System.out.println(new Greeting(users).hello("1")); // prints: hi Ana
System.out.println(new Greeting(users).hello("9")); // prints: hi guest
}
}Common Gotchas
A mock that only stubs is a stub
do not `verify` every call "for coverage".
Doubling the class under test proves nothing
Doubling the class under test proves nothing