Article 5H3ZD CodeSOD: Absolute Mockery

CodeSOD: Absolute Mockery

by
Remy Porter
from The Daily WTF on (#5H3ZD)

At a certain point, it becomes difficult to write a unit test without also being able to provide mocked implementations of some of the code. But mocking well is its own art- it's easy to fall into the trap of writing overly complex mocks, or mocking the wrong piece of functionality, and ending up in situations where your tests end up spending more time testing their mocks than your actual code.

Was Rhonda's predecessor thinking of any of those things when writing code? Were they aware of the challenges of writing useful mocks, of managing dependency injection? Or was this Java solution the best they could come up with:

public class Person{ private int age; private String name; public int getAge() { if (Testing.isTest) return 27; else return age; } public String getName() { if (Testing.isTest) return "John Smith"; else return name; } // and so on ..}

Every method was written like this. Every method. Each method contained its own mockery, and in turn, made a mockery of test-driven-development.

proget-icon.png [Advertisement] ProGet's got you covered with security and access controls on your NuGet feeds. Learn more. TheDailyWtf?d=yIl2AUoC8zAW-I5SNjzG9Y
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments