Testing Inheritance
Not sure you get the whole inheritance idea? Still not sure which fields and methods get inherited and which are overridden? Looking to test your understanding? (wink)
Knowing what we know now about unit-testing and inheritance , we can test that our subclasses extend their base classes.
We can add a test
folder to our inheritance
package and write some code to ensure that
HouseCat
inherits what we expect it to.
@Test
public void inheritsSuperInFirstConstructor() {
HouseCat keyboardCat = new HouseCat("Keyboard Cat", 7);
assertEquals(7, keyboardCat.getWeight(), .001);
}
Here, we’re testing that one of our HouseCat
constructors will call the Cat
constructor
and appropriately assign the HouseCat
object’s weight
property. Remember, we don’t need
to write unit tests for getters or setters unless they do something extra in addition to getting
or setting. The purpose of this test, though, is less to test getting keyboardCat.weight
and more to validate that the subclass constructor has inherited the base class constructor.
It’s a good practice to test your subclasses to verify the items that they inherit or override.
Check Your Understanding
Fill in the blank to test that the default constructor of Cat
is called when the second
constructor on HouseCat
is used.
Second HouseCat
constructor:
public HouseCat(String aName) {
name = aName;
}
@Test
public void inheritsDefaultCatInSecondConstructor() {
HouseCat keyboardCat = new HouseCat("Keyboard Cat");
<insert assertion method here>
}
assertEquals(13, keyboardCat.getWeight());
assertNotNull(keyboardCat.getWeight());
assertEquals(13, keyboardCat.getWeight(), .001);
assertNotNull(keyboardCat.weight);
What additional assert method can we add to this test to properly verify that HouseCat
inherits eat()
?
@Test
public void isNotInitiallyTired() {
HouseCat keyboardCat = new HouseCat("Keyboard Cat");
assertFalse(keyboardCat.isHungry());
assertFalse(keyboardCat.isTired());
keyboardCat.eat();
}
assertFalse(keyboardCat.isTired());
assertTrue(keyboardCat.isTired());
assertTrue(keyboardCat.isHungry());
assertFalse(keyboardCat.tired);