Questions tagged [stub]

A replacement implementation for objects, methods, or functions in order to remove external dependencies.

A replacement implementation for object, method, or function. The typical types of stubs are:

  • Testing stubs that remove external dependencies. They are typically used during unit and component testing. If you're trying to write a unit test and need to replace a simple call to a database, external libraries (e.g., file I/O) or other system API, stubbing might be perfectly suited for your needs.
  • RPC (CORBA, RMI, web service) stubs forward the call to remote service where it is handled; the answer, if any, is returned to the calling side. They implement the same interface that is implemented by the servicing object on remote side (may also implement additional interfaces).
  • Dummy stubs are empty placeholders for objects, methods or functions that are supposed to be completed later. They allow the current code to compile so that other, already finished parts could be tested. Such stubs are used when implementing a very large libraries following some already specified API.

A mock is somewhat similar to the testing stub but is not considered stub as it is a special purpose object capable of registering that has been called on it. It does not take other actions (a generally unfavoured partial mock does forwards execution of some methods to underlying object).

985 questions
1082
votes
39 answers

What's the difference between a mock & stub?

I've read various articles about mocking vs stubbing in testing, including Martin Fowler's Mocks Aren't Stubs, but still don't understand the difference.
never_had_a_name
  • 80,383
  • 96
  • 257
  • 374
828
votes
15 answers

Access restriction on class due to restriction on required library rt.jar?

I'm attempting to compile Java 1.4 code that was created by IBM's WSDL2Java on Java5 without recreating the stubs and saw this error in Eclipse. I'm under the assumption that the stubs generated should just compile as long as the runtime jars are…
sal
  • 22,528
  • 15
  • 64
  • 84
133
votes
11 answers

What does "to stub" mean in programming?

For example, what does it mean in this quote? Integrating with an external API is almost a guarantee in any modern web app. To effectively test such integration, you need to stub it out. A good stub should be easy to create and consistently…
janko-m
  • 7,958
  • 7
  • 31
  • 45
130
votes
6 answers

What is a "Stub"?

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 in to, so I wanted to check my understanding of what I…
Rob Cooper
  • 27,684
  • 25
  • 100
  • 142
108
votes
4 answers

Difference between Mock / Stub / Spy in Spock test framework

I don't understand the difference between Mock, Stub, and Spy in Spock testing and the tutorials I have been looking at online don't explain them in detail.
Wang-Zhao-Liu Q
  • 12,331
  • 27
  • 70
  • 106
87
votes
7 answers

How to stub process.env in node.js?

I want to stub process.env.FOO with bar. var sinon = require('sinon'); var stub = sinon.stub(process.env, 'FOO', 'bar'); I'm confused. I read document, but still I don't understand yet.sinonjs docs sinonjs is one example, not sinonjs is okay.
Matt - sanemat
  • 4,694
  • 4
  • 32
  • 39
83
votes
5 answers

Can RSpec stubbed method return different values in sequence?

I have a model Family with a method location which merges the location outputs of other objects, Members. (Members are associated with families, but that's not important here.) For example, given member_1 has location == 'San Diego (traveling,…
Mike Blyth
  • 3,816
  • 1
  • 28
  • 39
64
votes
8 answers

How do I stub things in MiniTest?

Within my test I want to stub a canned response for any instance of a class. It might look like something like: Book.stubs(:title).any_instance().returns("War and Peace") Then whenever I call @book.title it returns "War and Peace". Is there a way…
Nick
  • 641
  • 1
  • 5
  • 3
50
votes
3 answers

How do I stub new Date() using sinon?

I want to verify that various date fields were updated properly but I don't want to mess around with predicting when new Date() was called. How do I stub out the Date constructor? import sinon = require('sinon'); import should =…
MrHen
  • 2,222
  • 1
  • 23
  • 37
45
votes
3 answers

In RSpec, is there a method equivalent to "unstub" but for "should_receive"?

Is there any method to remove any stubbing and mocking while using RSpec? Example: RestClient.should_receive(:delete).with("http://www.example.com") ... ... # this will remove the mocking of "should_receive" and # restore the proper "delete"…
p.matsinopoulos
  • 7,374
  • 6
  • 41
  • 84
38
votes
2 answers

What is double method in rspec for?

It is stated in rspec doc that I should use double method in order to create test double. But I can see that it works perfectly ok even if I don't use double. Is there anything wrong with not using double? Also if I'm not using double how MyClass…
grafthez
  • 3,501
  • 4
  • 22
  • 41
36
votes
4 answers

Testing software: fake vs stub

There are quite a few written about stub vs mocks, but I can't see the real difference between fake and stub. Can anyone put some light on it?
Vadim Samokhin
  • 3,188
  • 2
  • 37
  • 62
36
votes
3 answers

meaning of RuntimeException("Stub!") in Android

I was surfing in Android code because I wanted to see what is into Activity.finish() method. I just wanted to have the confirmation that in Activity.finish() there would be a call to onDestroy() method. But what I found in this method (and in many…
Alex Mawashi
  • 1,686
  • 2
  • 21
  • 42
33
votes
3 answers

What is " Stub " and "AIDL" for in java?

Question 1: I am studying Android service and often see code like this: private ISampleService.Stub sampleServiceIf = new ISampleService.Stub(){} What is .Stub ? Question 2: I checked "AIDL", but I want to know why we have to use that instead of…
AmyWuGo
  • 2,105
  • 4
  • 20
  • 25
33
votes
7 answers

Django unit testing with date/time-based objects

Suppose I have the following Event model: from django.db import models import datetime class Event(models.Model): date_start = models.DateField() date_end = models.DateField() def is_over(self): return datetime.date.today() >…
Fragsworth
  • 28,413
  • 24
  • 76
  • 96
1
2 3
65 66