2

I have a singleton class with private constructor and want to write unit tests for that.

Any idea how to mock a class which has private constructor using mockito framework.

Thanks

TP_JAVA
  • 1,002
  • 5
  • 23
  • 49
  • 1
    Maybe this answer points into the right direction: http://stackoverflow.com/questions/3138153/how-does-mockito-create-an-instance-of-the-mock-object – Dennis Traub Aug 07 '12 at 13:58
  • Do you mean that you want to write tests for the singleton class, or tests for classes that use the singleton? – Paul Morie Aug 07 '12 at 14:16

2 Answers2

3

Whilst not answering your question directly, I think it would be worth reading about the singleton anti-pattern, and perhaps consider an alternative approach such as dependency injection with which to compose your application.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

Well you should be able to use this library: http://code.google.com/p/powermock/ to do it. But it is highly discouraged.

Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94