In the previous article we have seen how to unit test private instance methods. In this article we will see unit testing static private methods. If you have not read the previous article on unit testing private instance methods, I would strongly recomend you to read, before you proceed with this article.
In general, to unit test a static public method, we invoke the respective method using the
In the example below, CalculatePower() is a private static method. The purpose of this method is to calculate the value, when a given number is raised to a certain power. For example 2 to the power of 3 should return 8 and 3 to the power of 2 sholuld return 9. So to unit test this method we create the instance of PrivateType class. To the constructor of the PrivateType class we pass the type of the class that contains the private static method that we want to unit test. We do this by using the typeof keyword. The PrivateType instance can then be used to invoke the private static method that is contained with in the Maths class. The Maths class that contains the static private CalculatePower() method and the unit test are shown below.
public class Maths
private static int CalculatePower(int Base, int Exponent)
int Product = 1;
for (int i = 1; i <= Exponent; i++)
Product = Product * Base;
public void CalculatePowerTest()
PrivateType privateTypeObject = new PrivateType(typeof(Maths));
object obj = privateTypeObject.InvokeStatic("CalculatePower", 2, 3);