'Mocking "non-public members" of a .NET class
I'm working on creating unit tests for a component which utilizes the .NET class EventBookmark and am working towards creating a mocked object for said class. When I look inside the class I see the following:
public class EventBookmark : ISerializable
{
protected EventBookmark(SerializationInfo info, StreamingContext context);
[SecurityCritical]
protected virtual void GetObjectData(SerializationInfo info, StreamingContext context);
}
The strange thing is that when I inspect the object while debugging the component during a real run(not tests) I see additional "Non-public members" which aren't mentioned above in the EventBookmark object:

Where are these fields coming from? These fields are ultimately what I need my unit tests to use. Any advice on how to unit test something depending on a class similar to EventBookmark (no visible fields in class description, serializable) would be very helpful.
Solution 1:[1]
The internal field BookmarkText can clearly be seen on ReferenceSource :
internal string BookmarkText { get { return bookmark; } }
Note, bookmark is here another field (it got no access modifier, so it has the default, which is 'private field'.
I am not sure why you want to unit test a .NET (Framework) class as this already got loads of tests of its code. Maybe you are in a hunt for boosting code coverage ?
I would suggest you test your code, preferably your public methods and public members and via those try to cover as much of your code as possible. If you however still want to test these private or internal members as we have seen, you can use for example reflection.
Consider an instance of EventBookmark eb, here is how you can grab hold of that value:
var bookmarkTextValue = eb.GetType().GetField("BookmarkText",
BindingFlags.NonPublic | BindingFlags.Instance).GetValue(eb);
Once you have the FieldInfo, you can also do a SetValue to set it also. But I guess you have some good reasons for doing this. When it comes to Test-Driven-Development, I usually stear away from static, non-public methods or similar, I strive to make my code test friendly which means the methods I want to test also becomes public, at the same time considering encapsulation and protection of data. Consider what you want to expose in an interface as public members as a contract and test the members in that contract. The inner workings, like private fields and protected members should be tested via public members and methods.
I have though had situations writing code where I do not own the code (such as .NET / .NET framework code), where using Reflection sometimes has been handy just to double check the framework does what I expect..
But keep in mind that Microsoft probably has tested much of the code you are using. So to answer your questions :
- Read ReferenceSource page to see the EventBookmark class members/methods - you will find what you seek there
- If you really want to test private or protected members/methods in a framework class or similar and you cannot easily change the code, consider using reflection.
You could also create a helper extension method here to do the reflection some other place than in a unit test setup.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tore Aurstad |
