9

I'm wondering about a nice way to deal with a protected method in Junit.

Assuming I want to test a class called A which has a protected member and constructor. I understood that in order to test the class A I should write another class called ATest which might extend TestCase ( this should be mandatory in Junit3 ). Because I want to test a protected method and because A has a protected constructor, my test class ATest should also extend the class A where that method is implemented in order to be able to create that class and to access to the method.

could be a double inheritance from both classes a nice solution?

P.S I've already Known that in Junit 4 the inheritance from the TestCase might be avoided.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
  • Can you explain why you would,like to extend TestCase? I think you should be able to do what you are saying and extend A to publicize its protected methods. I've dine it with NUnit and it works just fine – cowboydan Oct 12 '12 at 00:10
  • possible duplicate of [junit & java : testing non-public methods](http://stackoverflow.com/questions/440786/junit-java-testing-non-public-methods) – Ciro Santilli OurBigBook.com Feb 11 '15 at 13:55

2 Answers2

17

To gain access to A's protected members, you can just put A and ATest in the same package.

matt b
  • 138,234
  • 66
  • 282
  • 345
1

Java doesn't allow multiple inheritance of implementation. You can implement multiple interfaces.

I would prefer using reflection to get at methods for testing that I don't want clients to know about. Works for private methods, too.

duffymo
  • 305,152
  • 44
  • 369
  • 561