Skip to search.
testdrivendevelopment · Test-driven Development

Group Information

  • Members: 3501
  • Category: Software
  • Founded: Feb 7, 2002
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Messages

  Messages Help
Advanced
Mocking a singleton   Message List  
Reply Message #16088 of 34997 |
Re: [TDD] Mocking a singleton

Mocking a singleton is relatively easy.

To begin with, singleton's are all alone in the world; don't be afraid
to point this out. Also, most singletons are created, get this, not even
in a factory, but merely in a factory method. Finally there is the
point that they are little more than dressed-up globals, and IME
experience cause more heartache than they're worth. You can point to
the many nasty screeds about singletons there are out on the net. All
in all, mocking it shouldn't be too hard. A word: if your singleton
has unusually large body parts, stoop to insulting that. Sometimes, to
be kind, you have to be cruel.

Hill





Sun Apr 30, 2006 8:33 pm

uly562000
Offline Offline
Send Email Send Email

Message #16088 of 34997 |
Expand Messages Author Sort by Date

Hello all. I have difficulty on testing a class that depends on a singleton. I have no right to refactor the singleton into factory. Here is the structure of...
Monang Setyawan
oversleptkitten Offline Send Email
Apr 28, 2006
3:36 am

Hi Monang, ... What do you mean by "I have no right to refactor the singleton... " ? Charlie...
Charlie Poole
cpoole98370 Offline Send Email
Apr 28, 2006
4:58 am

... I interpreted that as: "For reasons I don't want to get into, I can't change the singleton, so let's focus the dicussion on what I can do, instead of why I...
William E Caputo
logosity Offline Send Email
Apr 28, 2006
7:06 pm

I've had the same issue of late, and took a slightly different approach. I wrapped the Singleton in a Provider interface. That way, I could return a Mock of...
Anderson, Kelly
kellycoinguy Offline Send Email
Apr 28, 2006
10:32 pm

Excellent advice! A technique that I have found very useful in this situation when using rMock http://rmock.sf.net is using "intercept". If I would use it on ...
j0hlrogge Offline Send Email May 1, 2006
1:45 pm

... I forgot to mention that this singleton is a final class that doesn't implement any interface. Using your approach, the only one way to create a ...
Monang Setyawan
oversleptkitten Offline Send Email
May 2, 2006
11:30 pm

... Yes, so does rMock, unfortunately if your class is final you are most likely out of luck anyway (CGLIB that is used for bytecode modification in rMock,...
Joakim Ohlrogge
j0hlrogge Offline Send Email
May 3, 2006
7:14 am

... Create a new interface that has the same methods as the Singleton. Derive an adapter from that interface that delegates to the singleton. Make sure that...
Robert C. Martin
rmartinoma Offline Send Email
May 16, 2006
10:12 pm

If you are in this situation for .NET I have written a code generator that helps, Doubler (*http://tinyurl.com/ofbmu*). It is Reflector add-in. The relevant...
Jay Flowers
jfl0wers Offline Send Email
May 16, 2006
10:57 pm

... Make the singleton un-final. Making things final is always a problem because it means you can't mock them. -- ... Robert C. Martin (Uncle Bob) | email:...
Robert C. Martin
rmartinoma Offline Send Email
May 16, 2006
9:28 pm

Unless of course, he works at Microsoft... then you're just out of luck. ;-) -Kelly ________________________________ Make sure that all the classes use your...
Anderson, Kelly
kellycoinguy Offline Send Email
May 16, 2006
10:21 pm

usually you want something from the singleton -- encapsulate getting that thing in another method: class ClassUnderTest { public void aMethod(){ Foo foo =...
Jeffrey Fredrick
frogstar Offline Send Email
Apr 28, 2006
6:10 am

Yes, that's a great technique. The other thing that the OP can do once he's using it is start to pass a reference to the singleton around so that the code...
Michael Feathers
mfeathers256 Offline Send Email
Apr 28, 2006
12:32 pm

... Yes, good solution (I've thought about this kind of refactoring before). My next question is, is it a common practice to make a method as "protected" ...
Monang Setyawan
oversleptkitten Offline Send Email
May 2, 2006
11:29 pm

... Instead of creating a new class that overrides doSomething you can intercept it: ClassUnderTest classUnderTest = ...
Joakim Ohlrogge
j0hlrogge Offline Send Email
May 3, 2006
7:32 am

Hi, I was involved in modifying a Legacy system having a lot of Singletons. We followed following technique (as described in "Working Effectively with legacy...
unmesh_joshi Offline Send Email May 3, 2006
8:44 am

I'm slightly worried about the focus. You probably don't want to explicitly test that you have a singleton. You probably want to test if you set a given value...
Donaldson, John (GEO)
geo_johnfr Offline Send Email
Apr 28, 2006
2:57 pm

If you are lucky and the singleton implements an interface with the needed methods : Put it in a default protected attribute of the class that you can replace ...
Bart Reader
bartreader Offline Send Email
Apr 29, 2006
2:19 pm

Mocking a singleton is relatively easy. To begin with, singleton's are all alone in the world; don't be afraid to point this out. Also, most singletons are...
Michael Hill
uly562000 Offline Send Email
Apr 30, 2006
8:35 pm

... ;-> Ron Jeffries www.XProgramming.com I cannot find my duck....
Ron Jeffries
ronaldejeffries Offline Send Email
Apr 30, 2006
10:48 pm

... This is one of the primary reasons that singletons are problematic. They are very hard to mock, and it is hard to test systems that depend upon them. ...
Robert C. Martin
rmartinoma Offline Send Email
Apr 29, 2006
5:35 pm

Hi, One way I usually get around this is isolate the access to a singleton to a specific method in the class using it, then create a partial mock and mock that...
James Carr
cloud_strife... Offline Send Email
May 16, 2006
9:59 pm
Advanced

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help