Unit Testing Explicitly Hidden Methods

written by Michael McDonald on Thursday, February 28 2008

In this article I want to explain the differences between using the override modifier versus the new modifier on a method. I will also point out a specific reason why it is important to test the way you use published API's in your software via unit tests.

Below is some example code for reference during this article. A full sample is available on our forums in the “coding examples and solutions” section.
 

Sample Code 

public class BaseClass
    {
        public BaseClass() { }

        public virtual string GetMyClassName()
        {
            return "BaseClass";
        }
    }

    public class DerivedClassWithNewModifier : BaseClass
    {
        public DerivedClassWithNewModifier() { }

        public new string GetMyClassName()
        {
            return "DerivedClassWithNewModifier";
        }
    }

    public class DerivedClassWithOverrideModifier : BaseClass
    {
        public DerivedClassWithOverrideModifier() { }

        public override string GetMyClassName()
        {
            return "DerivedClassWithOverrideModifier";
        }
    }

Technical Definitions

The new modifier as explained by the MSDN:
When used as a modifier, the new keyword explicitly hides a member inherited from a base class. Hiding an inherited member means that the derived version of the member replaces the base-class version. Hiding members without the use of the new modifier is allowed, but generates a warning. Using new to explicitly hide a member suppresses this warning, and documents the fact that the derived version is intended as a replacement.


The override modifier as explained by the MSDN:
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

Summary

Let’s take a look at the code referenced earlier. We have two derived classes, one of which uses an override on the method GetMyClassName and the other uses a new modifier. When dealing with objects that are part of the same base type we often prefer to utilize polymorphism. The issue is that while the class with the override modifier will have its logic called after it has been up-casted due to late binding, the one using the new modifier will not.

Once we cast DerivedClassWithNewModifier to BaseClass the object will now invoke the logic contained in BaseClass’s method as we have explicitly hidden the method contained in the derived class. This allows for the developer to alter the behavior of an object’s method by casting it to its inherited interface or base class.

Subtle Bugs at Runtime

However when interacting with a published API the developer may not be able to tell that this behavior change is going to take place for this particular class and not the others that inherit from the same base. This can cause some hard to detect bugs in your software due to the fact that the new modifier suppresses warnings. This leaves you with only unit tests to prove correct functionality in the situation that this is not documented in the API.

Please refer to the full sample for a demonstration of this issue as exercised with NUnit tests.
 

Similar Posts

  1. VistaDB 3 and beyond
  2. The GC does not solve all memory leaks
  3. Strongly typed performance gains

Comments

  • Jason Short on on 2.28.2008 at 5:24 PM

    Jason Short avatar

    Good job Mike, this can definately be a subtle thing to detect and NUnit can help you to ensure all the test cases are handled correctly.

Comments are closed

Options:

Size

Colors