This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Singleton class - unit testing ?  RSS feed

 
Ranch Hand
Posts: 1490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to unit test Singleton class using junit or some other test frameworks ?
 
best scout
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kri,

I guess your real problem is how to test any classes with static methods and all these ugly things. The best idea is to completely avoid them

Another very helpful testing framework I found some time ago is JMockit. It offers tools to overcome all the typical problems like static methods, final classes or attributes, private members etc. With a good application design and test driven development this shouldn't be necessary very often but it can be very handy if you really need such functionality during tests. This should also make it possible to make singletons more accessible for test code.

But as I already said: Better just avoid it!

Marco
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PowerMock is another solution for testing static methods.
 
kri shan
Ranch Hand
Posts: 1490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These singleton classes are legacy classes in our java code. I do not want to re-write those singleton classes. We are using junit for unit testing. Is there a way to test singleton classes using junit test framework ?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've already been given two frameworks that help. What other information are you looking for?

I mean, testing the singleton class itself seems really easy--just call its methods in the test case and make sure you get what you want. Testing things that *use* singletons is where JMockit and PowerMock come in to play.
 
Marco Ehrentreich
best scout
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The frameworks David and I mentioned should be usable as add-ons for JUnit. So you could use your existing test infrastructure.

With only the JUnit framework it depends... What exactly do you want to test? The "singleton behavior"? Or just ordinary methods? Methods with or without side-effects? Just because a class is designed as a singleton doesn't necessarily mean that you can't test it as usual. Where do you get stuck when you try to test this class?

Marco

PS: OK, David was obviously faster But as you see from both of our posts there's really no magic involved in testing singletons in general. The difficulty is how to obtain an instance of your singleton class as David already pointed out.
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!