File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

No Singletons in TDD?

Hussein Baghdadi
clojure forum advocate

Joined: Nov 08, 2003
Posts: 3479

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
author & internet detective

Joined: May 26, 2003
Posts: 33108

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: No Singletons in TDD?
It's not a secret anymore!