The Artima Developer Community
Sponsored Link

Java Buzz Forum
Mockito makes mocking fluid interfaces easy

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Mathias Bogaert

Posts: 618
Nickname: pathos
Registered: Aug, 2003

Mathias Bogaert is a senior software architect at Intrasoft mainly doing projects for the EC.
Mockito makes mocking fluid interfaces easy Posted: Feb 28, 2013 6:16 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Mathias Bogaert.
Original Post: Mockito makes mocking fluid interfaces easy
Feed Title: Scuttlebutt
Feed URL:
Feed Description: tech gossip by mathias
Latest Java Buzz Posts
Latest Java Buzz Posts by Mathias Bogaert
Latest Posts From Scuttlebutt

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 fluid 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(""); when(navBuilder.repo(REPOSITORY).pullRequest(2).buildAbsolute()).thenReturn(""); The arguments to each method in the fluid interface determine which mocks are returned, so you can stub different return values, too. Easy!

Read: Mockito makes mocking fluid interfaces easy

Topic: ActiveMQ Clustering, Durable Subscribers, and Virtual Topics to the rescue Previous Topic   Next Topic Topic: Implementing custom Future

Sponsored Links


Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use