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: 350
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 9: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: http://feeds.feedburner.com/AtlassianDeveloperBlog
Feed Description: tech gossip by mathias
Latest Java Buzz Posts
Latest Java Buzz Posts by Mathias Bogaert
Latest Posts From Scuttlebutt

Advertisement
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("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 fluid interface determine which mocks are returned, so you can stub different return values, too. Easy!

Read: Mockito makes mocking fluid interfaces easy

Topic: IntelliJ IDEA 12.1 EAP 128.18 is Available Previous Topic   Next Topic Topic: Using Twitter4j with Scala to perform user actions


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us