This is an old revision of the document!
Table of Contents
Sources:
- Software Testing course at ITMO university by Kochubeev Nikolay Sergeevich
Test doubles
Test doubles are used in unit tests to mimic the real objects the tests depend on.
Test double types
There are several test doubles types:
- dummy;
- stub;
- fake;
- spy;
- mock.
Dummy
Dummy is an object that's not actually used in the test. It is used as a placeholder, for example, when there needs a certain type of object to be passed as a parameter to initialize a method.
Stub
Stubs provide predetermined responses. They're used to isolate the behavior of the component from its dependencies. They're typically used to simulate the behavior of complex or external components (data bases, web services, external API).
Fake
A fake is a simplified real object. They're used for tests that require a real implementation but a lighter and faster one.
Spy
Spies record information about how they were interacted with during the test (how many times was the method called, what arguments were passed, the order of method calls).
Mock
Mocks are used to verify that certain interactions took place by being pre-programmed to expect certain specification of the calls.
Anti-patterns
Over-mocking
If the mock expects way too specific calls, the test no longer checks for the behavior, but for the inner details of implementation.
Testing implementation instead of the behavior
When a test knows too much about the inner workings of the class (what methods are called and in what order). The slightest refactoring can break such a test.
