deidara song
deidara song

Reputation: 209

unit testing private class within a public class?

Is it possible to unit test a private class? I have something like

     public class ClassA: ClassB    
      {     
         private class ClassX: ClassY   
         { 
            public static int TestMethod(int numberA, int numberB) 
               { 
               return numberA + numberB
               }  
          }
       }

I'd like to test the "TestMethod" but can't access it. I tried to create new instance for the private ClassX using Class A and Class Y but it won't let me access the "TestMethod"

Upvotes: 0

Views: 1925

Answers (2)

Tilak
Tilak

Reputation: 30738

It is not a good practice to test private members. They should be tested using pubic exposed members.

However, If you are using MSTest, You can use accessors.

If you are using other test frameworks, you can leverage reflection.

The above are applicable in .NET. I have no idea how to achieve the same in other languages (in case you are asking for java/other language).

Upvotes: 2

Robert Greiner
Robert Greiner

Reputation: 29772

There should be a public method somewhere that calls the method in your private class. You should unit test that method. If you can't get to your private method through a public interface, then there is probably no need for the private method to begin with.

Upvotes: 1

Related Questions