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.
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...
... 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...
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...
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 ...
... 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 ...
... 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,...
... 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...
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...
... 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:...
Unless of course, he works at Microsoft... then you're just out of luck. ;-) -Kelly ________________________________ Make sure that all the classes use your...
usually you want something from the singleton -- encapsulate getting that thing in another method: class ClassUnderTest { public void aMethod(){ Foo foo =...
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...
... 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" ...
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...
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...
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 ...
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...
... 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. ...
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...