Stub Object

See also ShuntPattern, MockObject, MockStubShunt

The most simple implementation of an interface. In java, one might implement every method call with an empty (No-Op) body, returning null, or by throwing a runtime exception. This can be a useful Parent class to extend from to create Shunt and MockObjects.


A Stub Object is one whose methods are stubs (or "mocks"?); that is, they do no useful work but prevent #doesNotUnderstand errors and return plausible values so that the computation can continue. They are used during testing or prototyping, where attention is focused on that other computation.

Not to be confused with NullObject, although a Null Object can be used as a stub.


In Java, C++, it is useful to implement "actors" from your test cases as stub objects that derive from the interfaces on which a test depends. These stubs actually do a bit more than nothing: they can drive stimulus and check expected results. A TestCase may act as a mediator that coordinates/controls the behaviours of the actors.

See also EncapsulateNewForTestability

(Note: There's a set of pages to be refactored here: this one, TestingPatterns, MockObject, ShuntPattern, ...)

--DaveWhipp


one might implement a stub by extending an existing class and overriding every method.

but that's not as good as implementing an interface, since all of the base-class state will be initialized even when creating the stub; and in some cases (in for example some classes in the Java API) this initialization can have side-effects that might cause problems.--BrettNeumeier

Agreed; it also propagates dependencies - where the real class has members which must be initialized, their code must also be stubbed out, and this can build up quickly until there's more stubbing than testing. The easier solution is to create the minimal interface - essentially just the public functions excluding (con|de)structor - which is inherited by the real class, but can be instantiated as a stub in itself, or inherited instead by a mock class. -- RobDesbois?


CategoryMockObjects CategoryTesting


EditText of this page (last edited November 1, 2006) or FindPage with title or text search