Testing - Pt 4 - Test Support and Test Doubles

· testing oped

Document supporting code:

  1. Test doubles or fixtures won't be reused if people don't know about them or how.

With JUnit, consider using @Rules to provide mixin-esq components for tests.</span>

Prefer fakes:

  1. They're generally more versatile and reusable than stubs, dummies or mocks.
  2. They'll give you a better understanding of the subject than other types of doubles.
  3. They can often share a code with the implementation, and thereby test that as well.
  4. Have the ability to directly control fakes by an interface, e.g. to put components into error mode that cannot be stimulated by normal APIs, e.g. network issues or hardware failures.

Fake the third-party:

  1. In my job there's a fair amount of JNI/JNA code that talks to hardware. By faking just the JNI methods, we can simulate various things including timeouts of failures. I've done similar things with faking serial devices, faking javax.comm.SerialPort and pre-loading it with fake data that simulates failures or other errors.
  2. This will work equally as well with RESTful APIs and the like.

← Back to part 3 ~ On to part 5 →