Stub – a temporary placeholder
In software development, a stub refers to a temporary placeholder or substitute. It is a class that acts as an interface to a subject – a file, a resource, a storage object, or a network connection. As a representative of this subject, the stub can control the creation of the subject and the corresponding access to it. It is used, for example, when
- external services required (databases, web services, servers) are not available within the scope of programming.
- required program parts have not yet been implemented.
- Writing and subsequent deletion of test data in databases should be avoided during function tests.
Stubs are also used in the development of distributed systems. The required functionality of the remote software system is addressed as if it were available locally. However, the desired function is not implemented, but delegated to the remote system via the stub (Remote Procedure Calls). If the required data type is determined, a monomorphic stub is used, otherwise a polymorphic stub operates with a hash table that refers to a list of functions, but acts slower due to the necessary lookup.
Areas of application for stubs
In general, the term stub is used in various areas far away from software development:
- In the cooperative writing of articles, where contributors want or have to research details, for example.
- As an employer’s proof of the amounts he has paid employees (a so-called pay stub).
- As a period in the interest transaction that deviates from the usual intervals (a so-called stub period).
- In computer networks in which a network section with only one output router uses other networks.
- In electronics, this is the calculated length of the transmission line that is used to adjust the impedance in transmission lines.
The difference between stub and mock object
Again and again there are discussions about the difference between a stub and a mock object in testing. Some argue that the differences are marginal, since the purpose of both is to test all dependencies of a class or function; others refer to different expectations. Tests written with mocks usually follow an initialization -> setting expectations -> execution -> checking, while a stub follows an initialization -> exercise -> checking.
Here you will find additional information from our Smartpedia section: