user810430
user810430

Reputation: 11499

How check in junit method was invoked from super class or from descendant class in java?

Code:

 ClassBase {
    protected method1() {

     protected methodA (par1, par2){

     ... } }

  };
 ClassA  extends ClassBase {
   @Override
     protected methodA (par1, par2){
       .....
       };   
  };

After:

ClassA testClass=new ClassA();
 testClass.methodA();            

Is it possible to test (may be junit) that when running method methodA, if methodA from descendant classA was invoked ? Thanks.

Upvotes: 1

Views: 722

Answers (1)

Thomas Uhrig
Thomas Uhrig

Reputation: 31605

There isn't anything to test. If you have a ClassA object (you can easily test this) and you call methodA on it, you could be sure that the overriding method from ClassA is called. That is a guaranty of the Java language. It's even guarantied if you cast a ClassA object to its super class.

You can use the override annotation to make sure that you really override a method in ClassA, if you want to test this. Or you can test on the specific result. Your overriding method is probably doing something different than your overwritten method (if both methods to the same thing, it makes no sense to overwrite it). Test on that difference.

Upvotes: 1

Related Questions