Testing on the Toilet

Feb 19, 2009

Partial Mocks using Forwarding Objects A Partial Mock is a mock that uses some behavior from a real object and some from a mock object. It is useful when you need bits of both. One way to implement this is often a Forwarding Object (or wrapper) which forwards calls to a delegate. For example, when writing an Olympic swimming event for ducks, you could create a simple forwarding object to be used by multiple tests: interface Duck { Point getLocation(); void quack(); void swimTo(Point p); }

class ForwardingDuck implements private final Duck d; ForwardingDuck(Duck delegate) public Point getLocation() { public void quack() { public void swimTo(Point p) { }

Duck { { this.d = delegate; } return d.getLocation(); } d.quack(); } d.swimTo(p); }

And then create a test that uses all of the real OlympicDuck class's behavior except quacking. public void testDuckCrossesPoolAndQuacks() { final Duck mock = EasyMock.createStrictMock(Duck.class); // enforces call order mock.swimTo(FAR_SIDE); mock.quack(); // quack after the race EasyMock.replay(mock); Duck duck = OlympicDuck.createInstance(); Duck partialDuck = new ForwardingDuck(duck) { @Override public void quack() { mock.quack(); } @Override public void swimTo(Point p) { mock.swimTo(p); super.swimTo(p); } // no need to @Override “Point getLocation()” } OlympicSwimmingEvent.createEventForDucks() .withDistance(ONE_LENGTH) .sponsoredBy(QUACKERS_CRACKERS) // their slogan: “quack” .addParticipant(partialDuck) .doRace(); MatcherAssert.assertThat(duck.getLocation(), is(FAR_SIDE)); EasyMock.verify(mock);} partialDuck is a complex example of a partial mock – it combines real and mock objects in three different ways: ● quack() calls the mock object. It verifies that the duck doesn't promote the sponsor (by quacking) until ● ●

after the race. (We skip the real quack() method so that our continuous build doesn't drive us crazy.) getLocation() calls the real object. It allows us to use the OlympicDuck's location logic instead of rewriting/simulating the logic from that implementation. swimTo(point) calls both objects. It allows us to verify the call to the real duck before executing it.

There is some debate about whether you should forward to the real or mock Duck by default. If you use the mock duck by default, any new calls to the mock will break the test, making them brittle. If you use the real duck, some very sensitive calls like submitToDrugTest() might get called by your test if your duck happens to win. Consider using a Partial Mock in tests when you need to leverage the implementation of the real object, but want to limit, simulate or verify method calls using the power of a mock object.

More information, discussion, and archives: http://googletesting.blogspot.com Copyright © 2007 Google, Inc. Licensed under a Creative Commons Attribution–ShareAlike 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).

Partial Mocks using Forwarding Objects

Feb 19, 2009 - ... 2007 Google, Inc. Licensed under a Creative Commons. Attribution–ShareAlike 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).

174KB Sizes 1 Downloads 313 Views

Recommend Documents

Partial Mocks using Forwarding Objects
Feb 19, 2009 - quack after the race. EasyMock.replay(mock);. Duck duck = OlympicDuck.createInstance();. Duck partialDuck = new ForwardingDuck(duck) {.

PARTIAL SEQUENCE MATCHING USING AN ...
where the end point of the alignment maybe be unknown. How- ever, it needs to know where the two matching sequences start. Very recently [6] proposed an ...

Compressing Forwarding Tables
Abstract—With the rise of datacenter virtualization, the number of entries in forwarding tables is expected to scale from several thousands to several millions. Unfortunately, such forwarding table sizes can hardly be implemented today in on-chip m

Quantum Search Algorithm with more Reliable Behaviour using Partial ...
School of Computer Science. University of Birmingham. Julian Miller ‡. Department of Electronics. University of York. November 16, 2006. Abstract. In this paper ...

Regular Expression Matching using Partial Derivatives
Apr 2, 2010 - show that the run-time performance is promising and that our ap- ...... pattern matchings, such as Perl, python, awk and sed, programmers.

Using Partial Least Squares in Digital Government ... -
relationship to information technology success and few hypotheses ..... Percentage of population with bachelor's degree or higher (2000). -0.7734. Percentage of ...

Pursuit on a Graph Using Partial Information
instrumented node, the UGS therein informs the pursuer if ... If this happens, the. UGS is triggered and this information is instantaneously relayed to the pursuer, thereby enabling capture. On the other hand, if the evader reaches one of the exit no

Use EasyMock for, well, easier mocks - PDFKUL.COM
Dec 4, 2008 - sign the class under test needs refactoring. Basic EasyMock only mocks interfaces, but there is an EasyMockClassExtension that mocks non- ...

Use EasyMock for, well, easier mocks
Dec 4, 2008 - account.login(mockAtm);. // 5. assertTrue(account.isLoggedIn();. EasyMock.verify(mockAtm);. // 6. } We tell EasyMock to create a dynamic proxy ...

Compensation of Fading Channels using Partial ...
1 Department of Physics, Faculty of Sciences and Technology, Sultan Moulay Slimane University, Morocco. 2 Department of ... The characteristics of wireless signal changes as it travels from the transmitter ..... networks evolution and human.

Regular Expression Sub-Matching using Partial ...
Sep 21, 2012 - A word w matches a regular expression r if w is an element of the language ...... 2 Weighted does not support the anchor extension. In the actual bench- .... interface. http://www.cse.unsw.edu.au/~dons/fps.html. [5] R. Cox.

Partial identification of willingness-to-pay using shape ...
Jul 14, 2010 - For r>rn, it would have to decrease to take values lower than wn. Convexity rules θn out of the two triangular ˜C regions. Suppose the function passed through ...... Masters degree. 0.04. Professional degree. 0.01. PhD. 0.01. Less th

Use EasyMock for, well, easier mocks
Dec 4, 2008 - EasyMock.expect(mockAtm.enterAccount("MyAccount")).andReturn(true); // 2. EasyMock.expect(mockAtm.enterPin("1234")).andReturn(true);. // 3. EasyMock.replay(mockAtm);. // 4. Account account = new Account(); account.login(mockAtm);. // 5.

ELS Forwarding Letter.pmd - Malappuram District Panchayat
Items 1 - 16 - 16. Place: Signature of applicant ( with date) ... leave may fill in the columns and sign the application for and on behalf of the Government servant. 18.

ELS Forwarding Letter.pmd - Malappuram District Panchayat
Items 1 - 16 - resignation from service at any time until I earn half pay leave not less than the amount of leave not due availed of by me. 16. Place: Signature of ...