This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "No Singletons in TDD?" Watch "No Singletons in TDD?" New topic

No Singletons in TDD?

Hussein Baghdadi
clojure forum advocate

Joined: Nov 08, 2003
Posts: 3476

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?
[ August 06, 2008: Message edited by: John Todd ]
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30136

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
Consider Paul's rocket mass heater.
subject: No Singletons in TDD?
Similar Threads
Static Variables and Methods
Private constructors.
Implementing DAOs as singleton
Providing sessionbeans through init()