Should I unit test methods that are inherited from a superclass?

I'm currently writing an implementation of the JDBC driver (yes, you read it right) in a TDD manner, and while I just finished the cool stubs and just some minor features, it just seemed to me that since then Statement has been a superclass for PreparedStatement , which is a superclass for CallableStatement , what should I do when I really start writing tests for my implementations of these classes, which one I need to do:

  • Create a test case for Statement , and then extend this package for additional tests for PreparedStatement , and then do the same for CallableStatement .
  • Test each implementation separately, ignoring methods inherited from the superclass (s).
  • Strictly check each individual method for each implementation class; Perhaps some inherited methods work differently depending on the implementation. A soft change to this would be that I would check all of these inherited methods that the implementation uses.

Number two feels most natural, but because of the reason I put in third, I'm not sure it would be wise to do this. So what do you think I should do?

+7
java unit-testing junit tdd
source share
4 answers

I would not specifically make alternative 1 (letting the test class hierarchy be the same as the actual class hierarchy), if that means that you will repeat the same tests for each test subclass. I am also skeptical of the subclass's test class other than the general utility base class.

I usually do 1 test for each class in the hierarchy, abstractly or not. So the base class has a separate test (usually with the local local subclass test, which is used to test it), and I use my knowledge of the subclasses to write the proper tests for each subclass. I see that the coverage runs through what passes the tests, so I'm usually not too formalized.

+7
source share

"check each separate method for each implementation class"

In particular, the unacceptability of overriding a superclass method is a common mistake. The author of the subclass makes assumptions about the superclass. The superclass changes, and the subclass is now broken.

+8
source share

Provide enough tests to make you feel comfortable - based on your knowledge of the implementation. I do not consider unit testing as completely black testing. If you know that the base class never calls any virtual methods (or at least none of them are overridden), then pay attention to this fact, but do not effectively duplicate unit tests already performed.

Testing devices, of course, can be taken in extreme cases - it is always worth balancing the cost that you get from it, due to the efforts that cost you.

+3
source share

With TDD, you should not strive for testing methods, but the behavior or capabilities of your code. Therefore, when implementing a subclass, you can restrict testing to only behavior other than the base class. When in doubt, write a new test.

+2
source share

All Articles