Testing on the Toilet

June 26, 2008

Defeat "Static Cling" You're pair programming and, as many brilliant people are apt to do, talking out loud. "I'll make a mock, inject it, and rerun the test. It should pa- ...D'OH" Your partner notices the exception "ConnectionFactory not initialized". "What?" she says, "Something is using the database? Dang, and this was supposed to be a small test." Upon inspection you find that your class is calling a static method on some other class. You've got Static Cling! If you're (ab)using a data persistence layer that generates code which relies on static methods, and weren't careful, your code might look something like this: public class MyObject { public int doSomething(int id) { return TheirEntity.selectById(id).getSomething(); }

As a result, you can't call doSomething without calling TheirEntity's static method. This code is hard to test because static methods are impossible to mock in Java. So, how do you get rid of this form of Static Cling and get that small test to pass? You can use a technique sometimes known as the Repository Pattern, a form of Abstract Factory. Create an interface and an implementation with the unmockable static method calls: interface TheirEntityRepository { TheirEntity selectById(int id); // other static methods on TheirEntity can be represented here too } public class TheirEntityStaticRepository implements TheirEntityRepository { public TheirEntity selectById(int id) { // method not static return TheirEntity.selectById(id); // calls static method }

Next, inject a TheirEntityRepository into MyObject and use it instead of calls to the static method: public class MyObject { private final TheirEntityRepository repository; public MyEntity(TheirEntityRepository arg) { this.repository = arg; } public int doSomething(int id) { return repository.selectById(id).getSomething(); }

You can do this even if you don't have access to source code for TheirEntity, since you're not changing the source itself, but merely encapsulating its static methods in an injectable interface. The techniques shown here generalize to the case where a static method acts as a Factory of objects. Now you can inject different implementations of the repository for different tests, such as "never finds anything," "always throws an exception," "only returns a TheirEntity if the id is a prime," and so forth. These kinds of tests would've been impossible before this refactoring.

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

Testing on the Toilet

Jun 26, 2008 - If you're (ab)using a data persistence layer that generates code which relies on ... 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).

186KB Sizes 0 Downloads 203 Views

Recommend Documents

testng-on-the-toilet - NeoOffice Writer Code
20 Mar 2008 - TestNG is a test framework for Java unit tests that offers additional power and ease of use over JUnit. Some of TestNG's features will help you to write your PirateShip tests in such a way that you'll be well prepared to take on the Adm

testng-on-the-toilet - NeoOffice Writer Code
Mar 20, 2008 - TestNG's capacity for running tests in parallel. You can do this in the definition of ... in an XML file) with the parallel and thread-count attributes.

the toilet paper entrepreneur 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. the toilet paper ...

Testing on the Toilet's 100th Episode Spectacular!
Aug 14, 2008 - Testing Blog -- after all, the internal episodes had an 8-month head start, and many would make ... With episodes covering many programming.

Testing on the Toilet's 100th Episode Spectacular!
Aug 14, 2008 - (This week, TotT issued our 100th internally published episode. That's more than have been published to this. Testing Blog -- after all, the internal episodes had an 8-month head start, and many would make no sense to readers outside o

Testing on the Toilet's 100th Episode Spectacular!
Aug 14, 2008 - That's more than have been published to this. Testing Blog -- after all, ... TotT's weekly entertainment and testing advice. In this 100th episode, ...

Social cognition on the Internet - testing constraints on social ...
as such: it is about social complexity and the limits. placed on ... Page 3 of 10. Social cognition on the Internet - testing constraints on social network size.pdf.

Toilet - Fundamental Right.pdf
life, whether a citizen is entitled to civic. ::: Downloaded ... basic amenity of public conveniences drives the ... Displaying Toilet - Fundamental Right.pdf. Page 1 ...

Toilet Paper Roll Wreath.pdf
Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Toilet Paper Roll Wreath.pdf. Toilet Paper Roll Wreath.pdf. Open.

Overview of comments received on 'Guideline for the testing and ...
Jul 14, 2016 - Committee for Medicinal Products for Veterinary Use (CVMP). Overview of .... infestation. Single housing is recognised to cause stress in social ..... Res Tech. 2013 .... development program for selection of a field isolate for.

Draft guideline on the plant testing strategy for veterinary medicinal ...
May 27, 2016 - 383. •. Feed type, feeding regime and the veterinary history of the animals from which the manure. 384 originates (if data are available). 385.

Draft guideline on the plant testing strategy for veterinary medicinal ...
May 27, 2016 - Draft agreed by Environmental Risk Assessment Working Party (ERAWP) ..... Concept development for an. 219 .... solvent control containing manure and solvent, for application via spiked quartz sand (as little as. 303 possible) ...

Overview of comments received on 'Guideline for the testing and ...
Jul 14, 2016 - the 3Rs, the text could be elaborated on to clearly mention animal welfare, the definitions ...... Comment: The header is bold here whereas in the respective ..... http://www.cdc.gov/ticks/life_cycle_and_hosts.html. Chagas et al.

Draft guideline on the higher-tier testing of veterinary medicinal ...
Jul 25, 2016 - Endpoints include adult mortality, and the number and age stage of. 155 ... species are active in the region hosting the study (typically spring).

Toilet Repair Greenwood IN.pdf
Plumbing Services. Plumber. Plumbers Near Me. Toilet Repair. Emergency Plumber. Drain Cleaner. Local Plumbers. Water Heater Installation. Plumbing Companies. Blocked Toilet. Clogged Toilet. 24 Hour Plumber. Water Heater Repair. Page 3 of 4. Toilet Re

SD018R - MB3 toilet move - enlarged plan and rcp.pdf
SD018R - MB3 toilet move - enlarged plan and rcp.pdf. SD018R - MB3 toilet move - enlarged plan and rcp.pdf. Open. Extract. Open with. Sign In. Main menu.

Autotest — Testing the Untestable
the same services for kernel (and hardware) testing, this assumption is ..... Launch “network server” job on one machine ..... //www.ccur.com/isddocs/ANTS.pdf.

Google Self-Driving Car Testing Report on Disengagements of ...
fallen even as we drive more autonomous miles on public roads. ... primary and secondary (back-up) self-driving systems (for example, a broken wire); when .... number of events makes a trend hard to discern because an aberrational month ...