The stuntmen of unit testing – the test doubles

Test doubles are fake implementations which are used to replace real implementation during testing, for example to isolate the tested entity from the rest of the system. This enables testing without the use of real databases, services, and other complex and fragile dependencies – and let’s one focus the testing to the entity one is interested in.

There are various different types of doubles, for example fakes that have some logic build in, stubs that have fixed logic build in, and mocks that have expectations build in. There are times when to use one specific type – and there may be times when to not to use any of them at all.

Using test doubles is, at least in most of the cases, completely optional. They may make testing more stable and faster, though. But they can also become a challenge, if they’re used too naively.