In today's section, we'll see how to extend even inaccessible class and write test against that, but before that let us assume that you have the following class:
- internal class internalClass:baseClass
- {
- public string getFirstValue()
- {
- return "extension";
- }
- internal class secondClass
- {
- internal string getSecondValue()
- {
- return "smith";
- }
- private class thirdClass
- {
- private string getThirdValue()
- {
- return "dave";
- }
- }
- }
- }
Here, It's quite easy to write test for
getFirstValue() as in the following code snippet.
- [TestMethod]
- public void Class1Test()
- {
- var obj1 = new internalClass();
- Assert.AreEqual("extension",obj1.getFirstValue());
- }
And this will pass. Similarly, we can write test for other (next one) as well and that will also pass.
- [TestMethod]
- public void Class2Test()
- {
- var obj2 = new internalClass.secondClass();
- Assert.AreEqual("smith", obj2.getSecondValue());
- }
But, for the third one, which is private, we need to make use reflection as in the following code snippet:
- [TestMethod]
- public void Class3Test()
- {
- var type = typeof (internalClass.secondClass).GetNestedType("thirdClass", BindingFlags.NonPublic);
- var methodInfo = type.GetMethod("getThirdValue", BindingFlags.NonPublic | BindingFlags.Instance);
- var obj3 = Activator.CreateInstance(type);
- Assert.AreEqual("dave",methodInfo.Invoke(obj3,null));
- }
Here, using reflection, I got the access of that private method and then created instance and hence tested the same. However, let us suppose, you would like to extend this class and make more reusable. In real time project scenario, your extension could be more meaningful. But, here for demo purpose, I am just extending the class to modify the output.
- internal static class internalClassExtensions
- {
- public static string getFirstValueUpper(this internalClass objClass)
- {
- return objClass.getFirstValue().ToUpper();
- }
- }
In order to test the same, I can go ahead and write the test case as in the following code snippet:
- [TestMethod]
- public void Class1ExtensionTest()
- {
- var obj1 = new internalClass();
- Assert.AreEqual("EXTENSION", obj1.getFirstValueUpper());
- }
Very powerful and easy to use, right?
Similarly, we can go ahead and extend the second class as in the following code snippet:
- public static string getSecondValueUpper(this internalClass.secondClass objSecondClass)
- {
- return objSecondClass.getSecondValue().ToUpper();
- }
And, again test for the same will look like the following:
- [TestMethod]
- public void Class2ExtensionTest()
- {
- var obj2 = new internalClass.secondClass();
- Assert.AreEqual("SMITH", obj2.getSecondValueUpper());
- }
However, if we try to access the third class as in the following, it won't be accessible, since it's private.
Here, to rescue us again, reflection will come into picture. The following is the code snippet:
-
- public static string getThirdValueUpper(this object obj)
- {
- var upper = string.Empty;
- var type = typeof (internalClass.secondClass).GetNestedType("thirdClass", BindingFlags.NonPublic);
- if (obj.GetType() == type)
- {
-
- var method = type.GetMethod("getThirdValue", BindingFlags.NonPublic | BindingFlags.Instance);
- var result = method.Invoke(obj, null) as string;
- upper = result.ToUpper();
- }
- return upper;
- }
Similarly, we can write test for the same as in the following code snippet:
- [TestMethod]
- public void Class3ExtensionTest()
- {
- var type = typeof(internalClass.secondClass).GetNestedType("thirdClass", BindingFlags.NonPublic);
- var methodInfo = type.GetMethod("getThirdValue", BindingFlags.NonPublic | BindingFlags.Instance);
- var obj3 = Activator.CreateInstance(type);
- Assert.AreEqual("DAVE", obj3.getThirdValueUpper());
- }
Now let us go ahead and add one abstract class on the top of this and extend the same. The following is the complete code snippet:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ExtensionMethods.Classes
- {
- internal abstract class baseClass
- {
- public virtual string getFirstName()
- {
- return "rahul";
- }
-
- protected virtual string getSecondName()
- {
- return "sahay";
- }
- }
- internal class internalClass:baseClass
- {
- public string getFirstValue()
- {
- return "extension";
- }
- internal class secondClass:baseClass
- {
- public override string getFirstName()
- {
- return "John";
- }
-
- internal string getSecondValue()
- {
- return "smith";
- }
- private class thirdClass
- {
- private string getThirdValue()
- {
- return "dave";
- }
- }
- }
- }
- }
And, its extension will look like the following:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ExtensionMethods.Classes
- {
- internal static class internalClassExtensions
- {
- public static string getBaseStringUpper(this baseClass objClass)
- {
- return objClass.getFirstName().ToUpper();
- }
- public static string getFirstValueUpper(this internalClass objClass)
- {
- return objClass.getFirstValue().ToUpper();
- }
-
- public static string getSecondValueUpper(this internalClass.secondClass objSecondClass)
- {
- return objSecondClass.getSecondValue().ToUpper();
- }
-
-
-
-
-
-
-
-
- public static string getThirdValueUpper(this object obj)
- {
- var upper = string.Empty;
- var type = typeof (internalClass.secondClass).GetNestedType("thirdClass", BindingFlags.NonPublic);
- if (obj.GetType() == type)
- {
-
- var method = type.GetMethod("getThirdValue", BindingFlags.NonPublic | BindingFlags.Instance);
- var result = method.Invoke(obj, null) as string;
- upper = result.ToUpper();
- }
- return upper;
- }
- }
- }
And, its finished tests will be the following:
- using System;
- using System.Reflection;
- using ExtensionMethods.Classes;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- namespace Legacy.Tests.Extension_Tests
- {
- [TestClass]
- public class InternalClassTests
- {
- [TestMethod]
- public void Class1Test()
- {
- var obj1 = new internalClass();
- Assert.AreEqual("extension",obj1.getFirstValue());
- }
- [TestMethod]
- public void Class2Test()
- {
- var obj2 = new internalClass.secondClass();
- Assert.AreEqual("smith", obj2.getSecondValue());
- }
- [TestMethod]
- public void Class3Test()
- {
- var type = typeof (internalClass.secondClass).GetNestedType("thirdClass", BindingFlags.NonPublic);
- var methodInfo = type.GetMethod("getThirdValue", BindingFlags.NonPublic | BindingFlags.Instance);
- var obj3 = Activator.CreateInstance(type);
- Assert.AreEqual("dave",methodInfo.Invoke(obj3,null));
- }
- [TestMethod]
- public void Class1ExtensionTest()
- {
- var obj1 = new internalClass();
- Assert.AreEqual("EXTENSION", obj1.getFirstValueUpper());
- }
- [TestMethod]
- public void Class2ExtensionTest()
- {
- var obj2 = new internalClass.secondClass();
- Assert.AreEqual("SMITH", obj2.getSecondValueUpper());
- }
- [TestMethod]
- public void Class3ExtensionTest()
- {
- var type = typeof(internalClass.secondClass).GetNestedType("thirdClass", BindingFlags.NonPublic);
- var methodInfo = type.GetMethod("getThirdValue", BindingFlags.NonPublic | BindingFlags.Instance);
- var obj3 = Activator.CreateInstance(type);
- Assert.AreEqual("DAVE", obj3.getThirdValueUpper());
- }
- [TestMethod]
- public void baseClassTest()
- {
- var obj0 = new internalClass();
- Assert.AreEqual("rahul",obj0.getFirstName());
- Assert.AreEqual("RAHUL", obj0.getBaseStringUpper());
- }
- [TestMethod]
- public void Class2OverrideTest()
- {
- var obj0 = new internalClass.secondClass();
-
-
-
- Assert.AreEqual("John", obj0.getFirstName());
- Assert.AreEqual("JOHN", obj0.getBaseStringUpper());
- }
- }
- }
Makes sense, right? Therefore, when you run all the tests, it will flag tests as green as in the following screenshot:
There are many other tests other than these, that's why I have not highlighted the same. Thanks for joining me.
Thanks!