6

When you use composition, then you can mock the other objects from which your class-under-test depends, but when you use inheritance, you can't mock the base class. (Or can you?)

I generally try to prefer composition over inheritance, but sometimes inheritance really seems like the best tool for the job - well, at least until it comes to unit-testing.

So, how do you test inheritance? Or do you just trash it as untestable and use composition instead?

Note: I mostly use PHP and PHPUnit, so help on that side is most appreciated. But it would also be interesting to know if there are solutions to this problem in other languages.

5
  • I don't see how it would be possible to mock a superclass. That would have to be a language feature ihmo. What exactly are you trying to test? To me it sounds like reflection could be helpful to you?
    – Robse
    Sep 19, 2008 at 9:49
  • I also don't know how mocking a parent class would be possible. That's why I'm asking. But I don't think that it's impossible - very few things in computers are. Sep 19, 2008 at 11:12
  • I still don't understand your requirements. What's wrong / missing if you test the superclass methods with their own unit tests, and then the subclass methods with also their own, distinct tests? It sounds like you want to test the superclass methods again in the tests for the subclass? Why would you need to do that? Or am I missing something
    – Robse
    Sep 19, 2008 at 11:44
  • @Robse This only works, when subclass just adds new methods without overriding any of those in parent class. But when I override a method in superclass, then I need to both ensure that the new method now changes the behavior of superclass as it should, and at the same time doesn't screw up the behavior that it shouldn't effect. Sep 19, 2008 at 12:00
  • There is a nice example for the solution given by munificent. Let the test hierarchy resemble your class hierarchy.
    – GrGr
    Nov 3, 2008 at 9:23

5 Answers 5

5

Use a suite of unit tests that mirrors the class hierarchy. If you have a base class Base and a derived class Derived, then have test classes BaseTests and derived from that DerivedTests. BaseTests is responsible for testing everything defined in Base. DerivedTests inherits those tests and is also responsible for testing everything in Derived.

If you want to test the protected virtual methods in Base (i.e. the interface between Base and its descendent classes) it may also make sense to make a test-only derived class that tests that interface.

4

As long as you don't override public methods of the parent class, I don't see why you need to test them on all the subclasses of it. Unit test the methods on the parent class, and test only the new methods or the overriden ones on the subclasses.

3

The reason you use mock objects in composition is if the real objects do something you dont want to set up (like use sockets, serial ports, get user input, retrieve bulky data etc). You should always use real objects where possible. Mock objects are only for when the estimated effort to implement and maintain a test using a real object is greater than that to implement and maintain a test using a mock object. Your base class shouldnt be doing anything fancy like that!

So you dont have to test the inheritance. Presumably you are using the behaviour of the base class, so just test the derived class as you would normally - calling methods on both the base and derived class as appropriate for the test. This ensures that all intended behaviour of the derived class is tested.

Essentially, (most of the time) you test a derived class as if the base class is invisible.

1

Why should you mock the base class?

How can you create derived classes from a non-existent parent class?

Just test it as usual, but have the parent class.

I think you haven't told the whole story.

In addition, language features supposedly work (unless you're working with beta releases or so), so you don't need to test if the method actually exist in a derived class.

0

No, you don't. You just have to check that the overriden methods do what they should. It should in NO WAY impact the behaviour of your parent methods. If your parent methods start to fail, then it means that you missed bound conditions when testing them at the parent level.

2
  • I completely agree that subclass methods shouldn't affect the way parent class methods behave. That is why I have avoided accessing member variables of parent class and only used its public inteface. But when the subclass method calls method in parent class, then when testing the subclass method I also end up testing parent class method. Sep 19, 2008 at 13:13
  • You'r right, and that's exactly what I'm pointing too! As long as your override a method in a subclass, you HAVE TO unit test it. The fact that it call the parent method or not does not matter, just test that the result of the subclass' method is the one you expect, as well as the parent method behave like you want it. Moreover, as @metao pointed, there is no additional state set-up in your parent class that you did not catch in your subclass. And if this occurs, then you should reconsider using composition over inheritance.
    – gizmo
    Sep 19, 2008 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.