0

I'm trying to write a wrapper class for a protected method which I wish to unit test. The problem in my case is that the original method is declared as protected static new which prevents me from accessing the base method because it's static.

Is there any other way to write this wrapper method? If not which other options do I have to unit test this method without changing its scope?

EDIT: Code Added:

public class DerivedClassToTest : BaseClass
{
    protected static new Type_A MehodeToTest()
    {
        Type_A A = new Type_A
        {
            //DoSomething...
        };
        return A;
    }
}

How do i test MehodeToTest?

I tried to derive from the DerivedClassToTest in my Unitest so i can access the Protected methode but its static so i cant call base.MethodeToTest. How do i access the MethodeToTest

Thank you.

6
  • 5
    You have especially bad architecture for unit testing: 1) static methods 2) hidden methods with new keyword... Commented Jan 30, 2012 at 8:02
  • 1
    From what you describe, it should work fine. Please show the code that doesn't work for you. Commented Jan 30, 2012 at 8:11
  • @Kirill Polishchuk: I agree, but its a given Code (not new one) so refactoring at this point is involving too much changes, which is not wished since this code is allready in use. Commented Jan 30, 2012 at 8:38
  • I usualy dont UnitTest my private-protected methods. Only the the public ones. Commented Jan 30, 2012 at 8:40
  • 2
    @gdoron: Good for you, but this is not my question, anyway the normal way over public methods needs too much inializing so i start with the small functions, basically ur right but lets not go into this disscusion.. Commented Jan 30, 2012 at 8:44

2 Answers 2

5

You could write a wrapper in your unit test project to expose the method:

public class DerivedClassToTestWrapper : DerivedClassToTest
{
    public static Type_A MehodeToTestWrapped()
    {
        return DerivedClassToTest.MehodeToTest();
    }
}

and then in your unit test:

var actual = DerivedClassToTestWrapper.MehodeToTestWrapped();

Also I am not quite sure what was the design goal of using protected static new on a member but I must confess that I have never seen such access modifiers on a member so far.

Sign up to request clarification or add additional context in comments.

1 Comment

It works, thank you, i was trying using the base.MehodeToTest() from within my static methode which is wrong. thanx again
1

Your approach is fine, however the base qualifier works only with instance methods. You can access the static test method like you would any other static method:

public class HelperClassToAccessMethodeToTest : DerivedClassToTest
{
       public static Type_A MehodeToTestWrapped()        
       {            
           return DerivedClassToTest.MehodeToTest();        
       }  
}

FWIW, I think you are following the right path to "unlock" legacy code for testing this way.

2 Comments

Ur right, that was my mistak, i was trying the conventional way and didnt noticed the static issue, thank you.
You can just say return MethodToTest();. There's no need to qualify with the class name, but it's also no problem to do that. Your helper class inherits the method and does not hide it with a new method.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.