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:

navBuilder.repo(repository).pullRequest(pullRequest.getId()).buildAbsolute()

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:

when(navBuilder.repo(REPOSITORY).pullRequest(1).buildAbsolute()).thenReturn("http://url.to/project/repo/pull-request/1");
when(navBuilder.repo(REPOSITORY).pullRequest(2).buildAbsolute()).thenReturn("http://url.to/project/repo/pull-request/2");

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

Easy!