0

For my JUnit-tests I'm using frameworks like Hamcrest and Mockito but none of them give me Matchers or something like this to test private constructors.

I know there a ways to test this like descriped here:

But I have more then one class with private constructors, so I don't want to duplicate code. Before I write my own testtools I wanna ask if there is any framework which can test my private constructors and test the class has no other constructor etc.

[UPDATE]

Community
  • 1
  • 1
kdoteu
  • 1,527
  • 1
  • 20
  • 26

1 Answers1

0

What do you expect by testing the private constructor? E.g. you're trying to test a singleton you should have a method returning the instance. So you could test the constructor implicit.

Matthias Baumgart
  • 905
  • 1
  • 9
  • 22
  • Increase my code coverage and taking care about that there will never be a constructor. There is no Constructor need because all methods are static. – kdoteu Feb 20 '14 at 10:34
  • If it's never constructed, why test it?? – Bart Feb 20 '14 at 10:36
  • As I said for my test coverage and i want that there'll be no constructor ever.For Example i can ensure if one of my team members do some code there, they'll never add constructors. – kdoteu Feb 20 '14 at 10:44
  • @kdoteu - if you have only static utility methods, your unit tests should test just them! There is nothing what you really could test on a private constructor. You may write a test that (mis)uses some reflection magick to test if all constructors are private, but this gains you next to nothing. – Gyro Gearless Feb 20 '14 at 10:45
  • @kdoteu In case it is just for increasing the test coverage you should recap the reply in the post you just linked about the test coverage of a private constructor. In my view it is not worth to test to pimp the stats. – Matthias Baumgart Feb 20 '14 at 10:59
  • @mbeat I agree the fact it's stupid to this only to increase the coverage. But I want to ensure nobody will add some constructors to a class with only static methods. Therefore i need a test for this. – kdoteu Feb 20 '14 at 11:35
  • @GyroGearless i know the way to test private constructors. But i want to test this in more then one class. And as I wrote at the beginning before I write sth. on my own I want to ask if there is already sth done. – kdoteu Feb 20 '14 at 11:39
  • @kdoteu You could make the class abstract so it cannot be instantiated. Also, what would happens if somebody would actually add a constructor and instantiates the class? – Bart Feb 20 '14 at 12:02
  • @Bart but so nobody can extends this class and this should be possible. – kdoteu Feb 20 '14 at 13:41
  • Thank you all for your efforts, but I dont want to discuss if I should Test or not. I only wanted to know if there is any framework who is doing alle the stuff for me. – kdoteu Feb 20 '14 at 13:47
  • Sorry for the late reply, you could either exclude the method (more coverage) or making it accessible for tests (Not good idea) – Cabrra Sep 09 '19 at 18:46