Friday, August 30, 2013

Call private static method of a static class

Code to test:

public static class StaticClass
{
    private static String StaticMethod()
    {
        return "StaticMethodReturn";
    }
}

Unit test:


using System;
using System.Reflection;
using NUnit.Framework;

[TestFixture]
public class StaticClassTests
{
    [Test]
    public void StaticMethodTest()
    {
        MethodInfo methodInfo = typeof(StaticClass).GetMethod("StaticMethod", BindingFlags.Static | BindingFlags.NonPublic);
        String result = (String)methodInfo.Invoke(null, new Object[0]);

        Assert.AreEqual("StaticMethodReturn", result);
    }
}

No comments:

Post a Comment