164

So, carrying on with my new years resolution to get more in to TDD, I am now starting to work more with Rhino Mocks.

One thing I am keen to do is to make sure I really grok what I am getting into, so I wanted to check my understanding of what I have seen so far (and I thought it would be good to get it up here as a resource).

What is a "Stub"?

4
  • 7
    first you ask a question, and then you give the answer?
    – empi
    Commented Jan 20, 2009 at 21:55
  • 20
    As stated in the question - I am looking for verification of my understanding being correct, I wanted the answer to be here (be it mine or not) to provide a resource on this much-asked-but-rarely-well-answered question :)
    – Rob Cooper
    Commented Jan 20, 2009 at 21:57
  • 56
    Also answering ones own question is indicated in the FAQ as a valid way of using the site - it should be encouraged, in fact. Commented Jan 20, 2009 at 21:58
  • It's fine to answer your own questions, though the question itself has a bit too much noise (autobiography), and shows no effort to find an answer, no inclination of what the person has done to try to find the answer.
    – eric
    Commented Mar 28, 2015 at 12:16

6 Answers 6

156

Martin Fowler wrote an excellent article on this subject. From that article:

Meszaros uses the term Test Double as the generic term for any kind of pretend object used in place of a real object for testing purposes. The name comes from the notion of a Stunt Double in movies. (One of his aims was to avoid using any name that was already widely used.) Meszaros then defined four particular kinds of double:

  • Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
  • Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
  • Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.
  • Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

To put it in my own words: mock objects "expect" certain methods to be called on them, and typically cause a unit test to fail if their expectations aren't met. Stub objects provide canned responses (and can be autogenerated by helper libraries), but typically do not directly cause the unit test to fail. They are typically just used so that the object you're testing gets the data it needs to do its work.

3
  • 3
    Then what's the difference between fake objects and sutbs? As you stated, they have the same role to be implemented, isn't it?
    – LKM
    Commented May 31, 2016 at 5:35
  • 12
    @LKM a fake object is an actual object that implements real functionality and does things. Stubs are just "canned answers" eg. hard-coded JSON results that mimic what would be returned from the web server. Commented Feb 13, 2017 at 2:22
  • Dummy, fake, stub, mock, double, pretend, phantom, puppet, sentinel. New word after new word, until hell of new words.
    – shen
    Commented Apr 1, 2022 at 4:27
38

A "stub" is an implementation of an interface that exists to provide data/a response of some sort. For example:

  • a DataSet
  • list of Users
  • an Xml File

Normally this would be provided by another service (be it Web Service, another application, a database) but in order to improve the testability of the code, the results are "faked".

A major benefit of this is that it allows assertions to be made in unit tests based on the data expected. If errors arise due to data errors, then tests can easily be added, a new stub created (replicating the data error) and code produced to correct the error.

Stubs differ to Mocks in that they are used to represent and test the state of an object, whereas a Mock tests its interaction.

3
  • thanks for your answer, but i still do not understand: "an implementation of an interface"??
    – BenKoshy
    Commented Mar 17, 2016 at 22:03
  • It's like subclassing from a NetworkManager class and overwriting the methods you only care about so it can return back fixed data (instead of the code using the regular method). An example is overwriting a fetch request method that returns back a fixed data object instead of the test making an actual network call through the regular fetch request.
    – Jozey
    Commented Jun 29, 2021 at 14:50
  • Can you give an example of how you would test the state of an object? Commented 7 hours ago
16

I faced the question recently and recognised that this comparison between Stub and Driver is really clear and helpful:

Basically, stubs and drivers are routines that don’t actually do anything except declare themselves and the parameters they accept. The rest of the code can then take these parameters and use them as inputs.

+---------+-------------------------------+-------------------------------+
|         | Stub                          | Driver                        |
+---------+-------------------------------+-------------------------------+
| Type    | Dummy codes                   | Dummy codes                   |
+---------+-------------------------------+-------------------------------+
| Used in | Top Down Integration          | Bottom Up Integration         |
+---------+-------------------------------+-------------------------------+
| Purpose | To allow testing of the upper | To allow testing of the lower |
|         | levels of the code, when the  | levels of the code, when the  |
|         | lower levels of the code are  | upper levels of the code are  |
|         | not yet developed.            | not yet developed.            |
+---------+-------------------------------+-------------------------------+
| Example | A and B are components.       | A and B are components.       |
|         | A ---> B                      | A ---> B                      |
|         |                               |                               |
|         | A has been developed.         | A still needs to be developed.|
|         | B still needs to be developed.| B has been developed.         |
|         | Therefore, stub is used       | Therefore, driver is used     |
|         | in place of B to imitate it.  | in place of A to imitate it   |
|         |                               |                               |
|         | A ---> Stub                   | Driver ---> B                 |
+---------+-------------------------------+-------------------------------+

From Difference between Stub and Driver

7

I believe "stub" comes from STartUpBlock. it is used to refer to parts of code that are auto generated to help you, the developer, get started.

1
6

A "stub" or "stub method" is designed to be a starter-code or a temporary substitute for yet-to-be-developed code. It's a built-in code generated by an IDE. Stub methods are actually methods used for testing methods of a particular class. It is used by inputting some values for the local variables in your actual development methods and check if the output is correct. It is important in finding bugs in your code.

3
  • Thanks! I'm just new to programming and just started learning about Software Development. So far this is what I understand about stub methods. So I am not really sure what is its purpose, importance, and how it is used. But I think stub methods are actually methods used for testing methods of a particular class. It is used by inputting some values for the local variables in your actual development methods and check if the output is correct. It is important to find bugs to your codes. If you have something else to add or to correct, please do comment. I'm just currently learning about testing.
    – thox
    Commented Jan 13, 2018 at 5:30
  • 1
    Thank you so much for the guides and suggestions. Really appreciate it!
    – thox
    Commented Jan 13, 2018 at 5:45
  • This is actually is a correct answer. This is how the term was initially used. Pre-generation of some empty methods to be filled with correct code in the future. @EvanWeissburg all your questions are answered like : "It saves time". The difference from a "Mock" or a "Dummy" is in the "Stab" has no other purpose but to fill your required artifacts for software to be linked and compiled. The best example of a Stub is a minimal config-file without which application would not run, when installer generates this file for you it's a stub. :-) Commented May 3 at 8:06
0

After some research and based on stub files that I faced during my coder life, I would say that a stub file is just a file that contains a entire or part of the implementation of a file. It helps developers to start coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.