Mocking Non-public Members and Types
This article provides various examples that demonstrate how to mock non-public members and types with Telerik® JustMock.
Introduction
You can use JustMock to mock non-public members and types in elevated mode. That is useful when you want to isolate calls to non-public members and types, such as:
- private calls, methods, and interfaces
- private static methods and properties
- protected members
- internal classes
- internal virtual methods and properties
This feature is available only in the commercial version of Telerik JustMock. Refer to this topic to learn more about the differences between the commercial and free versions.
If you need a complete Visual Studio project that demonstrates how to mock non-public members and types, refer to the examples in the installation directory. The default installation directory is C:\Program Files (x86)\Progress\Telerik JustMock\Examples.
Prerequisites
In the next examples, we will use the following sample class to test:
Sample setup
Important
To mock non-public members and types, you first need to go to elevated mode by enabling JustMock from the menu. Learn how to do that in the How to Enable/Disable Telerik JustMock topic.
Step by Step Description
The following list describes the steps you should perform in order to mock a non-public member:
- Create an instance of the type that you want to test.
- Use the
Mock.NonPublic
modifier to mock a non-public member. - Add the arrange statement:
- Pass the target object to test.
- Pass the member name that you want to test as a
string
. - If you test a method, pass the arguments.
Example 1 shows how to set up that a call to the DoPrivate
method of the Foo
class must set a local variable called
. This way you override the original method behavior with the one that you specify.
Example 1: Change the behavior of a private method
Mock.NonPublic
can be also used to mock generic non-public methods. In addition to the non-generic method mock, the generic type arguments must be supplied in the arrangement.
Example 2: Change the behavior of a generic non-public method
Private Members
Private Methods with Parameters
Example 3 shows how you can arrange a call to a private method accepting an argument that matches any integer value. The example arranges the PrivateEcho
to return 1
when called with any int
parameter. In the acting phase, the PrivateEcho
method is called with 5
as argument.
For more details on how to work with parameters when mocking, check the Matchers help topic.
Example 3: Change the behavior of a non-public method with parameter
Private Methods with Overloads
In this section, you will find how to arrange a call to a private method with two overloads. The following class will be used as an example:
Sample setup
Important
To interact with non-public classes, you must add the
InternalVisibleTo
property inside the AssemblyInfo.cs in the project you need to test, like this:
In the sample setup shown above, the pExecute
method has two overloads - one without arguments and one accepting an integer value as an argument. The code in Example 4 mocks the overload that accepts an integer. The behavior of the method is arranged to set a local boolean variable to true
once it is called with 10
as argument. After that, it acts by calling foo.Execute(10)
and verifies that called
is true
.
Example 4: Mock private method with overloads
Private Implementation of an Interface Method
This section shows how you can mock an explicit (not public) interface implementation method from current or base class.
The following classes will be used:
Sample setup
As you can see from the sample above, the IManager
interface defines the Provider
property, which is then implemented in FooBase
. The concrete implementation we need to test, however, resides in the Bar
class, which uses the FooBase.Provider
property. In Example 5 you will see how you can mock the Provider
property.
Example 5: Mock the implementation of a private method defined in an interface
Internal Virtual Members
Internal Virtual Method
Mocking internal virtual methods uses similar approach to mocking public members. To demonstrate how you can use JustMock to mock an internal virtual method, we will be using the Do
method from the sample setup in the beginning of this topic.
Example 6: Mock internal virtual method
Note that, in the arrange statement, it is not used a mock of Foo
, but the actual instance.
Internal Virtual Property Get And Set
Arranging an internal virtual property is also similar to arranging a public property.
Example 7: Mock internal virtual property get
Note that, in the arrange statement, it is not used a mock of Foo
, but the actual instance.
The following code is an example of mocking internal virtual property set. The code overrides the actual implementation by arranging that the foo.Value
must be called with a certain value.
Example 8: Mock internal virtual property set
Private Static Members
Private Static Method
The following example shows how to mock a private static method. We use the following sample class:
Sample setup
Important
To interact with non-public classes, you should add the
InternalVisibleTo
property inside the AssemblyInfo.cs in the project you need to test, like this:
The method arranged in Example 9 is FooInternalStatic.EchoPrivate()
.
Example 9: Mock private static method
The code in Example 9 calls the Echo
method, but its implementation calls the EchoPrivate
method, so the assertion passes.
Like with non-public instance generic methods, Mock.NonPublic
can be used to mock non-public static generic ones.
Example 10: Mock non-public static generic method
Private Static Property
This section shows how to mock the get function of a private static property. The Foo class defined above is used as a sample setup.
The property arranged in Example 11 is Foo.PrivateStaticProperty
. When called, it will return an expected integer value, different from the default one.
Example 11: Mock private static property
To act, the code calls the GetMyPrivateStaticProperty()
method, but its implementation returns the PrivateStaticProperty
, so the assertion passes.
Protected Members
To show how to mock a protected member, we will use the following class:
Sample setup
To mock protected members in JustMock, you can use the same method and logic, as for the rest non-public types, previously shown in this topic.
First, we will arrange that our IntValue()
method must never occur::
Example 12: Arrange protected method
The second test will test if the protected Load()
method is actually been called, when Init()
is initiated.
Example 13: Arrange that a protected method must be called at least once
Important
To mock a protected type, your assembly name must be fully qualified according to the framework design rules, i.e.
assembly name = namespace
. Note that you can't mock types frommscorlib
in this way.
Internal Class
Let's see an example of how to mock an internal class from .NET. Consider the System.Net.HttpRequestCreator
class, which is internal, but has a public interface System.Net.IWebRequestCreate
. Example 14 mocks its Create
method.
Example 14: Mock internal class
Note the use of Arg.Expr.IsAny<Uri>()
- as we mock a non-public call, we need to know the type of the argument to resolve the method. Thus, instead of using Arg
, like we do in most of the other cases, we must use Arg.Expr
.
Important
To mock an internal type, your assembly name must be fully qualified according to the framework design rules, i.e.
assembly name = namespace
. Note that you can't mock types frommscorlib
in this way. JustMock does a hierarchical search to find the proper qualified name as in the above example.System.Net.HttpRequestCreator
is found in theSystem
assembly, not inSystem.Net
.
Example 15 shows how you can handle an even more complex scenario - mock internal class by calling an original constructor with arguments, then make a call to the original implementation from a public interface. For the purpose of the example, it will be used another internal class - System.Net.WebSocketHttpRequestCreator
- derived from the public interface System.Net.IWebRequestCreate
.
The sample test verifies whether the call to Create
method has been made and the returned Web.WebRequest
object has an expected value for RequestUri
property set.
Example 15: Complex mocking of internal .NET class with constructor arguments
Mocking Using a Dynamic Wrapper
JustMock leverages .NET 4 and the DLR to allow you to wrap non-public implementations in dynamic
objects and arrange them as naturally as you can do it with public members.
Example 16 shows how to wrap the mock instance in a dynamic object using Mock.NonPublic.Wrap()
. The wrapper can be passed to Mock.NonPublic.Arrange
and Mock.NonPublic.Assert
together with an operation to specify what you want to arrange. You could also arrange:
- the value of a property getter
- the action of a property setter
Argument matchers are specified using Arg.Expr
.
When using dynamic expressions in Visual C# projects, you should reference the Microsoft.CSharp assembly.