How to unit test abstract classes

36,524

Solution 1

If there are methods on this abstract class that are worth testing, then you should test them. You could always subclass the abstract class for the test (and name it like MyAbstractClassTesting) and test this new concrete class.

Solution 2

There are two opposite points of view:

  • Do not test abstract class itself, test concrete classes inherited from it
  • Abstract class should be tested as well because provides some built in logic shared across all the inherited classes so you just test base logic in abstract class once

I prefer second option (currently) and testing abstract classes using RhinoMocks PartialMock feature which allows me to create a mock of an abstract class.

Solution 3

  1. Just test the implementing classes.

  2. You could always create a specific implementation for testing that adds no extra functionality.

  3. Listen to the tests. Using mocking tools that do magic to allow testing abstract classes and private methods etc. are a test code smell

Share:
36,524
ediblecode
Author by

ediblecode

Casual gamer, part-time genius and programmer working in London

Updated on July 13, 2022

Comments

  • ediblecode
    ediblecode almost 2 years

    Used the create unit tests tool in Visual Studio and obviously it tries to instantiate my abstract classes.

    My question is: Should I try to unit test the way Visual Studio is trying to get me to do it, or should I create a mock class to be instantiated, or should I only test the methods that use this abstract class?

    Thanks.

  • Darkproduct
    Darkproduct almost 3 years
    All code that is written should have tests, regardless of whether it is used or not. If you have implementations that overwrite some abstract implementations you have to test those as well and not skip the abstract implementation.
  • Darkproduct
    Darkproduct almost 3 years
    If an abstract class implements any method then this method should be tested. It's not that hard to understand. An abstract class can have method implementations that can be overwritten by class implementations. This means, that you have to make sure to test the original implementation from the abstract class as well.
  • Eru
    Eru almost 3 years
    That is the point that you don't have to test this within the abstract class! You have derived from abstract class, you did NOT overwrite that implemented method and you test this within the tests for your derived class, the result will be correct. But you are testing the derived class not the abstract. But I suppose that you can have many different approaches and trying to find the "golden egg" is like discussing what is better: C# or Java :)