How to Write Unit Test For Private Static Method in C#
Tags: dotnet, coding, unittest, programming
Published: 2023-05-03 07:02:11


Testing private static methods in C# can be a tricky task as they are not directly accessible from outside the class. However, it is still possible to test these methods using various techniques. In this blog post, we will explore some of the ways to test private static methods in C#.

Why test private static methods?

Before diving into the testing techniques, let's first discuss why it is important to test private static methods. Private static methods are usually used to encapsulate some common logic or helper methods that are used within the class. These methods are not accessible from outside the class, so they cannot be tested directly. However, these methods can still contain bugs that may affect the behavior of the public methods that use them. Therefore, it is important to test these methods to ensure that they work as intended and do not introduce any bugs.

Techniques for testing private static methods

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgoc9rqyy72t6hi8nfco.png)
1. Reflection

Reflection is a powerful feature in C# that allows us to inspect and manipulate the metadata of types at runtime. Using reflection, we can access private members of a class, including private static methods. Here's an example of how we can test a private static method using reflection:

```
`[Test]
public void TestPrivateStaticMethod()
{
    // Arrange
    Type type = typeof(MyClass);
    MethodInfo method = type.GetMethod("MyPrivateStaticMethod", BindingFlags.NonPublic | BindingFlags.Static);

    // Act
    int result = (int)method.Invoke(null, new object[] { 2, 3 });

    // Assert
    Assert.AreEqual(5, result);
}`

```
In this example, we use the `GetMethod` method of the `Type` class to retrieve the private static method called `MyPrivateStaticMethod` from the `MyClass` class. We then use the `Invoke` method of the `MethodInfo` class to invoke the method and pass in the necessary parameters. Finally, we assert that the result of the method is what we expect it to be.

2. Extract method to a public method

One simple way to test a private static method is to extract the code into a public method in the same class and test it directly. Here's an example:

```
`private static int MyPrivateStaticMethod(int a, int b)
{
    return a + b;
}`

`public int MyPublicMethod(int a, int b)
{
    return MyPrivateStaticMethod(a, b);
}
`
```

In this example, we've extracted the code from `MyPrivateStaticMethod` into a new public method called `MyPublicMethod`. We can now test this method directly, which will indirectly test `MyPrivateStaticMethod`.

3. Test through public methods

Another way to test a private static method is to test it through public methods that use it. Since private static methods are usually helper methods used within the class, they are usually called from one or more public methods. By testing the public methods that use the private static method, we can indirectly test the private static method as well.

Conclusion

Testing private static methods in C# can be challenging, but it is important to ensure that our code is bug-free and behaves as expected. By using reflection, extracting the method into a public method, or testing through public methods, we can effectively test private static methods and ensure that they work as intended.