Get hands-on training for JIRA Software, Confluence, and more at Atlassian Summit Europe. Register now ›

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:

[cc lang=”java” line_numbers=”0″]
navBuilder.repo(repository).pullRequest(pullRequest.getId()).buildAbsolute()
[/cc]

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:

[cc lang=”java” line_numbers=”0″]
NavBuilder mockNavBuilder = mock(NavBuilder.class, RETURNS_DEEP_STUBS);
[/cc]

This allows you to stub pull request urls like this:

[cc lang=”java” line_numbers=”0″]
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”);
[/cc]

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

Easy!

Fresh ideas, announcements, and inspiration for your team, delivered weekly.

Subscribe now

Fresh ideas, announcements, and inspiration for your team, delivered weekly.

Subscribe now