Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I unit test methods which are inherited from super class?

I'm currently writing an implementation of a JDBC driver (yes, you read that correctly) in a TDD manner and while I have only finished class stubs at this point and only some minor functionality, it just occured to me that since Statement is a superclass for PreparedStatement which is a superclass for CallableStatement, what should I do when I really start to write tests for my implementations of those classes, which one of these should I do:

  1. Create a test suite for Statement and then extend that suite for additional tests for PreparedStatement and then do the same for CallableStatement.
  2. Test each implementation individually ignoring the methods inherited from superclass(es).
  3. Rigorously test every single method individually for each implementation class; It is possible that some inherited methods work differently depending on implementation after all. Mild variation of this would be that I'd test all those inherited methods the implementation uses.

Number two feels the most natural but due to the reason I put to the third one I'm not that sure if it'd be wise to do so. So, what do you think I should do?

like image 746
Esko Avatar asked Jan 01 '09 19:01

Esko


People also ask

What base class should test classes inherit from?

Whenever you're testing a subclass of your base class, have your test class inherit from your test superclass.

Does a subclass inherit methods from superclass?

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Should I unit test an abstract class?

The answer is: always test only concrete classes; don't test abstract classes directly . The reason is that abstract classes are implementation details. From the client perspective, it doesn't matter how Student or Professor implement their GetSignature() methods.

What inherits variables and methods of a superclass?

The subclass inherits state and behavior in the form of variables and methods from its superclass. The subclass can just use the items inherited from its superclass as is, or the subclass can modify or override it.


1 Answers

"test every single method individually for each implementation class"

In particular, failure to override a superclass method properly is a common bug. The author of the subclass makes assumptions about the superclass. The superclass changes, and the subclass is now broken.

like image 155
S.Lott Avatar answered Sep 19 '22 06:09

S.Lott