9

Can any one explain what is ongoing Stubbing in mockito and how it helps writing in Junit Testcase and mocking the methods.

Gowtham Murugesan
  • 407
  • 2
  • 6
  • 17
  • 1
    Are you referring to the class [`OngoingStubbing`](http://docs.mockito.googlecode.com/hg/org/mockito/stubbing/OngoingStubbing.html)? Your question is a little unclear. – Duncan Jones Mar 26 '15 at 14:36

1 Answers1

11

OngoingStubbing is an interface that allows you to specify an action to take in response to a method call. You should never need to refer to OngoingStubbing directly; all calls to it should happen as chained method calls in the statement starting with when.

// Mockito.when returns an OngoingStubbing<String>,
// because foo.bar should return String.
when(foo.bar()).thenReturn("baz");

// Methods like thenReturn and thenThrow also allow return OngoingStubbing<T>,
// so you can chain as many actions together as you'd like.
when(foo.bar()).thenReturn("baz").thenThrow(new Exception());

Note that Mockito requires at least one call to an OngoingStubbing method, or else it will throw UnfinishedStubbingException. However, it doesn't know the stubbing is unfinished until the next time you interact with Mockito, so this can be the cause of very strange errors.

// BAD: This will throw UnfinishedStubbingException...
when(foo.bar());

yourTest.doSomething();

// ...but the failure will come down here when you next interact with Mockito.
when(foo.quux()).thenReturn(42);

Though it is technically possible to keep around a reference to an OngoingStubbing object, that behavior is not defined by Mockito, and is generally considered a very bad idea. This is because Mockito is stateful, and operates via side effects during stubbing.

// BAD: You can very easily get yourself in trouble this way.
OngoingStubbing stubber = when(foo.bar());
stubber = stubber.thenReturn("baz");
stubber = stubber.thenReturn("quux");
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • 2
    _keep around a reference to an OngoingStubbing object_, as _end_ user just don't do it. It is not designed this way. The only sensible reason to have a reference to `OngoingStubbing` is when one is developing a functionality on top of mockito, it requires the knowledge of the inner working of mockito. – bric3 Mar 27 '15 at 12:24
  • Is it still true? Given link doesn't exist and docuemntation https://static.javadoc.io/org.mockito/mockito-core/2.2.9/org/mockito/stubbing/OngoingStubbing.html doesn't mention that we should never need to refer to OngoingStubbing directly? – user Oct 03 '18 at 14:19
  • @user Thanks; the link was just to the page you linked in your comment. To be clear, OngoingStubbing is a crucial part of Mockito's DSL (domain-specific language); you will _interact frequently_ with OngoingStubbing, and call methods defined on it and returned through `when`. That said, as Mockito contributor Brice confirmed above, you should not need to _save the OngoingStubbing instance to a field_ or _refer to OngoingStubbing by name at all_ (unless, say, you were wriiting a Mockito extension API like PowerMock). – Jeff Bowman Oct 03 '18 at 14:56