GeeCON Prague 2014*
The moose likes Testing and the fly likes No Singletons in TDD? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Testing
Bookmark "No Singletons in TDD?" Watch "No Singletons in TDD?" New topic
Author

No Singletons in TDD?

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hey,
An advice in the TDD isn't to write code that drive us to employ the Singleton pattern as it imposes itself as a problem in testability.
But Singleton pattern is very important and important (Spring DAOs for example are singletons).
Why not starting developing the singleton class with fake implementation and start refactoring it to the real implementation, you know, this is the TDD after all?
Does this mean that I don't use singleton in my TDD application?
Thanks.
[ August 06, 2008: Message edited by: John Todd ]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30596
    
154

John,
Aren't Spring DAO singletons dependency injected? The singleton itself isn't the problem per se. The problem is how to get a reference/interface that isn't a singleton. For example, when you really need a singleton, putting it behind a factory often helps.

TDD also points out that many things that are singletons don't need to be. Some people have "object creation phobia" and make classes singletons when they could easily be normal objects.

I don't always TDD, but I did try TDD'ng a singleton once. It wound up being something that didn't need to be a singleton and I learned not to presume the implementation. Of course, this doesn't apply to all cases.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: No Singletons in TDD?