Test doubles are objects used in place of real objects during unit testing. The document discusses different types of test doubles including dummies, stubs, spies, fakes, and mocks. It provides descriptions of each type and examples of when and how they may be used in unit tests. The key differences between the test doubles are also compared.
8. Example
/// pseudo code for CheckAuthentication
/// input: string id
/// input: string password
/// output: true --> if pass the checking
/// false --> else fail the checking
/// get the password's hash result from DAO
AccountDAO dao = new AccountDao();
String passwordHashFromDAO =
mDao.getPasswordHash(id);
mLog.writeLog(passwordHashFromDAO );
/// get the id's hash result from Hash
Hash hash = new Hash();
String hashResult =
mHash.getHashResult(password);
mLog.writeLoghashResult ();
/// check the result
return passwordHashFromDAO.equals(hashResult);
9. Example -- Dummy
• Dummy Object
o Passed around but never actually used. Usually,
they are just used to fill parameter list.
10. Example -- Stub
• Stub
o Provide canned answers to calls made during the
test, usually not responding at all to anything outside
what's programmed in for the test.
11. Example -- Fake
• Fake
o Actually have working implementations, but usually
take some shortcut which makes them not suitable
for production.
12. Example -- Spy
• Spy
o Stubs that also record some information based on
how they were called. One form of this might be an
email service that records how many messages it
was sent.
13. Example -- Mock
• Mock
o pre-programmed with expectations which form a
specification of the calls they are expected to
receive.
They can throw an exception if they receive a call
they don't expect and are checked during
verification to ensure they got all the calls they were
expecting.