Declarative Stubbing

Sureassert UC offers several ways of isolating code to be tested from separate but dependant code.

Test Doubles

Sureassert UC provides the @TestDouble annotation to define Test Double Classes in your application.  Classes annotated with @TestDouble replace a named class in the Sureassert testing context (when executing Exemplars or JUnits).  Test Doubles are especially effective for replacing classes designed to call out to external resources not available during unit testing.  A Test Double class can define behavior to intelligently respond to calls to the replaced class with data appropriate within the testing context.

Test Doubles need not share the same interface or class hierarchy with the doubled class because they totally replace the doubled class within the testing context rather than extend it.  Any class whose source is defined within the workspace can be doubled using @TestDouble.

  • Stub any class in your workspace with another
  • All Exemplars and JUnits use test double class with no configuration required
  • No requirement for matching class hierarchies between stub and replaced class

Method Stubs

Method stubs provide fine-grained stubbing on a per-test basis.  Any Exemplar or JUnit can define expressions to invoke in place of the code within specified methods. The stubbed method can belong to any class whose source is defined within the workspace.

The stub expression can return a simple value, build a complex object or invoke another method.  Different expressions can be executed or values returned on each sequential invocation of the stubbed method during test execution.  There are no constraints on whether a stub is executed or how many times.

  • Prevent methods from being executed during unit testing and force thier return values from within your Exemplar or JUnit (via @Stubs annotation)
  • Fine-grained declarative control of stubbed methods on a per-test basis.
  • Provide multiple expressions to cater for multiple invocations of a stubbed method.
  • Specify argument value matcher expressions for fine-grained control over which invocations of a method get stubbed.
  • Perform behaviour verification on both stubbed and non-stubbed code.

Source Stubs

Source stubs provide the most fine-grained stubbing option: replacement and/or insertion of code to be executed during Exemplars and JUnits within your project classes.  Source stubs are defined within special in-line comments in your code to prevent them interferring with normal (live) execution of your code.

Data from the Exemplar or JUnit under execution is made available to source stubs which can then alter the behaviour of code under test accordingly.

  • Prevent code lines and blocks within your code from being executed during Exemplar or JUnit runs using simple //[@Stub …] notation.
  • Replace lines or blocks of live code with test code for use in the unit-testing context.
  • Leave live code in place and insert your own code for use in the unit-testing context.
  • Make your stub conditional on the presence of data from the Exemplar or JUnit under test.
  • Insert dynamic expressions from the Exemplar or JUnit under test into your code for use within the unit-testing context.

Comments are closed.