(J)Unit testing abstract classes and interfaces
2Sep2008
Liskov-substituted ducks |
Note to self: This is how you can unit test abstract classes and interfaces in Java (using the Liskov substitution principle). Using a Java 5 generic for the type of the concrete class under test to avoid excessive casting seems to be a good idea. (AbstractDuckTest has the generic type T, getDuckUnderTest() returns a T, the concrete subclasses of the test case make T be the concrete tested class.)
In languages with dynamic typing, it doesn't get so complicated. If superclass attributes are accessible from classes below, you don't even need the getter. :-) Implementation becomes trivial then.
See Ward's wiki: Abstract Test Cases for a more detailed discussion, including code examples in Java.