Testing on the Toilet

Dec 4, 2008

Use EasyMock for, well, easier mocks Most of us are aware that mock and stub objects can make testing easier by isolating the class under test from external dependencies. This goes hand-in-hand with dependency injection. Writing all these classes can be a pain though. EasyMock provides an alternative. It dynamically implements an interface which records and replays your desired behavior. Let's say you want to model an ATM interface: public interface Atm { boolean enterAccount(String accountNumber); boolean enterPin(String pin); boolean enterWithdrawalAmount(int dollars); }

It is pretty easy to mock this interface. Still, every mock has to implement all three methods, even if you only need one. You also need a separate mock for each set of inputs. With EasyMock, you can create mocks as you need them, recording and replaying your expectations: public void testAtmLogin() { Atm mockAtm = createMock(Atm.class); EasyMock.expect(mockAtm.enterAccount("MyAccount")).andReturn(true); EasyMock.expect(mockAtm.enterPin("1234")).andReturn(true); EasyMock.replay(mockAtm); Account account = new Account(); account.login(mockAtm); assertTrue(account.isLoggedIn(); EasyMock.verify(mockAtm); }

// // // //

1 2 3 4

// 5 // 6

We tell EasyMock to create a dynamic proxy implementing Atm (1), which starts in record mode. Then we record two method calls along with the expected results (2 and 3). The replay()call tells EasyMock to stop recording (4). After that, calls on the object return the set values. If it gets a call it does not expect, it throws an Exception to fail fast. Account now uses the mock as if it were the real thing (5). The verify() method checks to see if the mock actually received all the calls you expect (6). It really is that simple. If we want to simulate failure, we can set up another test to return false from one of the method calls. EasyMock has lots more capabilities as well. It can throw exceptions. It also can record multiple calls to the same method returning the same or different results. You also can create stub expectations and nice mocks so you don't have to record every expected call. You also can create several mocks, and even nest them to test classes with complex dependencies. Beware, though, this often creates brittle tests, and is a sign the class under test needs refactoring. Basic EasyMock only mocks interfaces, but there is an EasyMockClassExtension that mocks non-final classes when you really must. See the EasyMock documentation at the link below for details.

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/).

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 ...

140KB Sizes 5 Downloads 175 Views

Recommend Documents

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 - 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.

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/).

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

Honor the Priesthood and Use It Well
which we can exercise the power of the priesthood ... intent is not to criticize but to help increase the ... Relief Society, you can call upon the elders quorum or the ...

27. Easier travel.pdf
adventure cruise into the little visited Pacaya-Samiria. Reserve, the largest protected wetland ... Easier travel.pdf. 27. Easier travel.pdf. Open. Extract. Open with.

Apparatus for well completion operations
Oct 20, 1986 - Primary Examiner-Stephen J. Novosad. Assistant Examiner-Terry Lee Melius. Attorney, Agent, or Firm-James R. Duzan. [57]. ABSTRACT.

Applying for California State Disability Insurance Now Easier ... - EDD
Page 1. Applying for California State Disability Insurance. Now Easier With Online Services. Page 2.

Directions For Use - GitHub
Page 7 of 46. 4. Using EMPOP to perform mtDNA haplotype frequency estimates. EMPOP follows the revised and extended guidelines for mitochondrial DNA typing issued by the DNA commission of the ISFG (Parson et al. 2014). See document for further detail

Switching is easier than ever
Back up your files, photos and music. First, grab your old ... computer and SD cards to safely store them in the cloud. ... Manager (https://play.google.com/music/.

Data Sharing Made Easier through Programmable Metadata
Commonly used big data workflow. • Slow, stale and strenuous. Primary Data: transactions, emails, logs, etc. Cloud analytics cluster. In-house analytics cluster ...

Digital marketing made (much) easier
Benefits of Google Tag Manager ... instead of marketing technology—so you can run your campaigns when ... Manager can help your business, please.

Digital marketing made (much) easier
tracking, site analytics, remarketing, and more—with just a few clicks; no more waiting ... Tag Manager can make your job easier. It lets you focus on ... Free and easy tag management. Want to focus ... Manager can help your business, please.

Make your videos easier to discover
make sure that your videos are discovered by users? ... We do this using our video index, which holds all of Google's knowledge about videos on the web.

Digital marketing made (much) easier.
analytics, remarketing, and more—with just a few clicks; no more waiting weeks (or ... be trademarks of the respective companies with which they are associated.

KALI KEDUA easier version.pdf
Page 1 of 3. KALI KEDUA. Raisa. Not Angka oleh Stephani A. Do = C. 4/4. INTRO. C F. { J3k.3 j.k45 . . | J3k.3 j.k45 . . } VERSE. C F G C G/B. j.k.5 j5k55 j.6 j7! | # j.6 . . | j.k.5 j5k55 k.k6k.7 k.k!k.5 | . . . . |. (Modulasi Do = Eb). Am Dm G Gm C7

mindfulness for well-being
Page 1 ... meanings we make in relation to that experience. • so as to relieve suffering? HOW IT ALL ... YOUR OWN PRACTICE TODAY. • Be present. • Feel your ...

Well Logging for Earth Scientists.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Well Logging for ...

Shower Filter For Well Water.pdf
This will also help you become more productive the next day. Page 3 of 6. Shower Filter For Well Water.pdf. Shower Filter For Well Water.pdf. Open. Extract.

Requirements for Well Wiring.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Requirements ...