Test Doubles

Test doubles:

Test doubles are lightweight versions of components that are necessary to test some class or system, usually designated the System Under Test(SUT).

Why Create Test Doubles?

  • The real components are not yet finished.
  • It is computationally expensive to construct the real components.
  • The real components are owned by someone else.
  • You want to avoid side effects on real components as a result of testing.
  • Test doubles help prevent Flaky Tests: tests that work sometimes but not all the time.
  • Test doubles allow finer-grained observation of the system under test.


    Test doubles provides Ecosystem:
  • Dummy objects:
    'fill in' objects required by the system under test(SUT) that are otherwise irrelevant for the test.
  • Test Stubs:
    It provide 'dummy' input data sources used by the SUT.
  • Fake objects:
    These are lightweight implementations of 'heavyweight' processes like database.
  • Mock Objects:
    It check indirect results produced by the SUT.

Tools for creating Test doubles:

Mockito

Mockito is a popular framework for creating test doubles in Java. It integrates well with jUnit for constructing unit test cases. It supports all the different kinds of test doubles. Goal is to be able to mock away dependencies for unit test and inspect interaction between SUT and mocks.

Comments

Popular Posts