Stubs, Expectations and the Dispatch of Mocked Methods

In jMock, stubs and expectations are basically the same thing. A stub is just an expectation of zero or more invocations. The stubs() method is syntactic sugar to make the intent of the test more explicit.

When a method is invoked on a mock object, the mock object searches through its expectations from newest to oldest to find one that matches the invocation. After the invocation, the matching expectation might stop matching further invocations. For example, an expects(once()) expectation only matches once and will be ignored on future invocations while an expects(atLeastOnce()) expectation will always be matched against invocations.

This scheme allows you to:

However, there are some possible "gotchas" caused by this scheme:

The best thing to do is not set up multiple expectations and stubs for the same method with exactly the same matchers. Instead, use the onConsecutiveCalls method to create multiple actions for a method. For example:

mock.expects(atLeastOnce()).method(m).with(...)
   .will( onConsecutiveCalls(
       returnValue(10),
       returnValue(20),
       throwException(new IOException("end of stream")) ) );

If you want to specify more complex ordering or order invocations across different mock objects, use the after method to explicitly define a total or partial ordering of invocations.

Document History