Whilst trying to TDD a bug fix, I hit a problem stubbing Stash’s NavBuilder. Stash uses NavBuilder to generate hyperlinks to different areas in the application. NavBuilder uses a hierarchy of builders to give developers a fluent interface for link creation. 

For example, building a link to a pull request looks like this:


Stubbing with mocks in the traditional way requires 3 mock objects: a mock for the NavBuilder, a mock for the NavBuilder.Repository returned by repo(), and a mock for the NavBuilder.PullRequest returned by pullRequest().

Instead, Thomas Bright clued me in to Mockito’s deep stubs. To make a deep stub, just add the RETURNS_DEEP_STUBS constant when creating the mock:

NavBuilder mockNavBuilder = mock(NavBuilder.class, RETURNS_DEEP_STUBS);

This allows you to stub pull request urls like this:


The arguments to each method in the fluent interface determine which mocks are returned, so you can stub different return values, too.